1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2009 Denys Vlasenko
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 /* For testing against dieharder, you need only random.{c,h}
12 * gcc -O2 -Wall -DRANDTEST random.c -o random
13 * ./random | dieharder -g 200 -a
20 # define RAND_BASH_MASK 0x7fff
27 # define FAST_FUNC /* nothing */
28 # define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN /* nothing */
29 # define POP_SAVED_FUNCTION_VISIBILITY /* nothing */
30 # define monotonic_us() time(NULL)
32 # define RAND_BASH_MASK 0xffffffff /* off */
36 next_random(random_t *rnd)
38 /* Galois LFSR parameter:
41 enum { MASK = 0x8000000b };
42 /* Another example - taps at 32 31 30 10: */
43 /* enum { MASK = 0x00400007 }; */
45 /* Xorshift parameters:
46 * Choices for a,b,c: 10,13,10; 8,9,22; 2,7,3; 23,3,24
47 * (given by algorithm author)
57 if (UNINITED_RANDOM_T(rnd)) {
58 /* Can use monotonic_ns() for better randomness but for now
59 * it is not used anywhere else in busybox... so avoid bloat
61 INIT_RANDOM_T(rnd, getpid(), monotonic_us());
64 /* LCG: period of 2^32, but quite weak:
65 * bit 0 alternates beetween 0 and 1 (pattern of length 2)
66 * bit 1 has a repeating pattern of length 4
67 * bit 2 has a repeating pattern of length 8
70 rnd->LCG = 1664525 * rnd->LCG + 1013904223;
73 * period of 2^32-1 = 3 * 5 * 17 * 257 * 65537.
74 * Successive values are right-shifted one bit
75 * and possibly xored with a sparse constant.
77 t = (rnd->galois_LFSR << 1);
78 if (rnd->galois_LFSR < 0) /* if we just shifted 1 out of msb... */
82 /* http://en.wikipedia.org/wiki/Xorshift
83 * Moderately good statistical properties:
84 * fails the following "dieharder -g 200 -a" tests:
87 * diehard_count_1s_byt| 0
94 * rgb_minimum_distance| 3
95 * rgb_minimum_distance| 4
96 * rgb_minimum_distance| 5
105 t = rnd->xs64_x ^ (rnd->xs64_x << a);
106 rnd->xs64_x = rnd->xs64_y;
107 rnd->xs64_y = rnd->xs64_y ^ (rnd->xs64_y >> c) ^ t ^ (t >> b);
109 * Period 2^64-1 = 2^32+1 * 2^32-1 has a common divisor with Galois LFSR.
110 * By skipping two possible states (0x1 and 0x2) we reduce period to
111 * 2^64-3 = 13 * 3889 * 364870227143809 which has no common divisors:
113 if (rnd->xs64_y == 0 && rnd->xs64_x <= 2)
116 /* Combined LCG + Galois LFSR rng has 2^32 * 2^32-1 period.
118 * individually, both are extremely weak cryptographycally;
119 * when combined, they fail the following "dieharder -g 200 -a" tests:
120 * diehard_rank_6x8| 0
123 * diehard_count_1s_byt| 0
127 * Combining them with xorshift-64 increases period to
128 * 2^32 * 2^32-1 * 2^64-3
129 * which is about 2^128, or in base 10 ~3.40*10^38.
130 * Strength of the combination:
131 * passes all "dieharder -g 200 -a" tests.
133 * Combining with subtraction and addition is just for fun.
134 * It does not add meaningful strength, could use xor operation instead.
136 t = rnd->galois_LFSR - rnd->LCG + rnd->xs64_y;
138 /* bash compat $RANDOM range: */
139 return t & RAND_BASH_MASK;
145 int main(int argc, char **argv)
151 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) {
152 buf[i] = next_random(&rnd);
154 write(1, buf, sizeof(buf));