3.43 mrand - Generate Random Numbers

Generate random number from the range 0.0 to 0.1, or generate random integers from a defined range. Define output column name at a= parameter.

This command uses Mersenne twister (developed in 1937) as pseudo random number generator. (Webpage of author , boost library).

Format

mrand [k=] a= [max=] [min=] [S=] [-int] [i=] [o=] [-nfn] [-nfno] [-q] [--help] [--version]

Parameters

k=

Same random number is generated for same key value at the specified key field.

a=

New column name. [However, this parameter is not required when -nfn,-nfno option is specified]

max=

Maximum value of random number [default value: INT_MAX]

 

Integer up to 0\UTF{FF5E}2^32 [21 billion] ).

 

-int must be specified with this parameter.

min=

Minimum value of random number [default=0].

 

Integer up to 0\UTF{FF5E}2^32 [21 billion]).

 

-int must be specified with this parameter.

S=

Random seed [default value: current time]

 

The same random seed generates the same random number

 

When S= is not specified, the default setting of random seed is set to the current time.

 

Random seed value can be specified between -2147483648〜2147483647.

-int

Generate random integers.

Examples

Example 1: Basic example

Generate random real numbers between 0.0 to 1.0.

$ more dat1.csv
Customer
A
B
C
D
E
$ mrand a=rand i=dat1.csv o=rsl1.csv
#END# kgrand a=rand i=dat1.csv o=rsl1.csv
$ more rsl1.csv
Customer,rand
A,0.6892393918
B,0.1042782064
C,0.07767942664
D,0.6829032891
E,0.9111980933

Example 2: Basic Example 2

Generate random integers with -int.

$ mrand a=rand -int i=dat1.csv o=rsl2.csv
#END# kgrand -int a=rand i=dat1.csv o=rsl2.csv
$ more rsl2.csv
Customer,rand
A,646494551
B,1824612880
C,1125031143
D,741644395
E,532560940

Example 3: Specify the minimum and maximum value of the random number

Generate a random number with a minimum value of 10 and maximum value of 100. Add the random numbers to a new column named rand.

$ mrand a=rand -int min=10 max=100 S=1 i=dat1.csv o=rsl3.csv
#END# kgrand -int S=1 a=rand i=dat1.csv max=100 min=10 o=rsl3.csv
$ more rsl3.csv
Customer,rand
A,47
B,100
C,75
D,94
E,10

Example 4: Generate random number by key

Given 4 customers A,B,C,D, same random number is generated for same customer.

$ more dat2.csv
Customer
A
A
A
B
B
C
D
D
D
$ mrand k=Customer -int min=0 max=1 a=rand i=dat2.csv o=rsl4.csv
#END# kgrand -int a=rand i=dat2.csv k=Customer max=1 min=0 o=rsl4.csv
$ more rsl4.csv
Customer%0,rand
A,0
A,0
A,0
B,0
B,0
C,0
D,0
D,0
D,0

Related Command

mselrand : Select a random record.

mnewrand : Generate new random dataset without using input file.