3 # Generate a random number in such a way that the sequence of numbers
4 # generated by successive calls can be deterministic.
8 # --let $rand_type= { int | float | decide }
9 # [--let $rand_min= MIN_VALUE]
10 # [--let $rand_max= MAX_VALUE]
11 # [--let $rand_probability= THRESHOLD]
12 # [--let $rand_seed= SEED]
13 # --source include/rand.inc
14 # --echo Random number: $rand
19 # If set to 'int', generates an integer in the range
20 # [$rand_min, $rand_max).
22 # If set to 'float', generates a floating point number in the
23 # range [$rand_min, $rand_max).
25 # If set to 'decide', generates 1 with probability
26 # $rand_probability and 0 with probability 1-$rand_probability.
28 # $rand_min, $rand_max
29 # Range of values for $rand_type=int or $rand_type=float.
32 # Probability to get a '1' if $rand_type=decide.
35 # The seed value. If you want to set the seed to a random number
36 # that is different for each invokation of the test, set
37 # $rand_seed to the empty string. Normally, $rand_seed is only
38 # read in the first invokation of rand.inc. If you want to reset
39 # the seed later, set $_rand_state to the empty string.
41 # ==== Implementation ====
43 # This RNG uses the following multiply-with-carry algorithm:
47 # z = 36969 * (z & 65535) + (z >> 16);
48 # w = 18000 * (w & 65535) + (w >> 16);
49 # rand = (z << 16) + w;
50 # In this implementation, z is stored in the low 32 bits and w in the
51 # high 32 bits of $_rand_state.
53 --let $_maxint32= ((1 << 32) - 1)
55 if ($_rand_state ==
'')
59 --let $rand_seed= `SELECT FLOOR(RAND() * (1 << 63))`
61 --let $write_var= $rand_seed
62 --let $write_to_file= $MYSQLTEST_VARDIR/tmp/rand_seed
63 --source include/write_var_to_file.inc
64 --let $_rand_state= $rand_seed
65 --let $_rand_lo= IF(($_rand_state) & $_maxint32 = 0, 1, ($_rand_state) & $_maxint32)
66 --let $_rand_hi= IF(($_rand_state) >> 32 = 0, 1, ($_rand_state) >> 32)
67 --let $_rand_state= `SELECT ((36969 * $_rand_lo + ($_rand_lo >> 16)) & $_maxint32) + (((18000 * ($_rand_hi & 65535) + ($_rand_hi >> 16)) & $_maxint32) << 32)`
70 --let $_rand_lo= IF(($_rand_state) & $_maxint32 = 0, 1, ($_rand_state) & $_maxint32)
71 --let $_rand_hi= IF(($_rand_state) >> 32 = 0, 1, ($_rand_state) >> 32)
72 --let $_rand_state= `SELECT ((36969 * $_rand_lo + ($_rand_lo >> 16)) & $_maxint32) + (((18000 * ($_rand_hi & 65535) + ($_rand_hi >> 16)) & $_maxint32) << 32)`
73 --let $rand= ((($_rand_state << 16) + ($_rand_state >> 32)) & $_maxint32)
75 if ($rand_type == decide)
77 --let $_rand_probability= 0.5
78 if ($rand_probability !=
'')
80 --let $_rand_probability= $rand_probability
82 --let $rand= `SELECT IF($rand / (1 << 32) < ($rand_probability), 1, 0)`
84 if ($rand_type != decide)
89 --let $_rand_min= $rand_min
94 --let $_rand_max= $rand_max
96 if ($rand_type ==
int)
98 --let $rand= `SELECT ($_rand_min) + ($rand % (($_rand_max) - ($_rand_min)))`
100 if ($rand_type ==
float)
102 --let $rand= `SELECT ($_rand_min) + ($rand * (($_rand_max) - ($_rand_min)) / (1 << 32))`