1 /* ----> DO NOT REMOVE THE FOLLOWING NOTICE <----
\r
3 Copyright (c) 2014-2015 Datalight, Inc.
\r
4 All Rights Reserved Worldwide.
\r
6 This program is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; use version 2 of the License.
\r
10 This program is distributed in the hope that it will be useful,
\r
11 but "AS-IS," WITHOUT ANY WARRANTY; without even the implied warranty
\r
12 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 GNU General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with this program; if not, write to the Free Software Foundation, Inc.,
\r
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
19 /* Businesses and individuals that for commercial or other reasons cannot
\r
20 comply with the terms of the GPLv2 license may obtain a commercial license
\r
21 before incorporating Reliance Edge into proprietary software for
\r
22 distribution in any form. Visit http://www.datalight.com/reliance-edge for
\r
26 @brief Implements a random number generator.
\r
29 #include <redtestutils.h>
\r
32 /* This is the global seed used by the random number generator when the caller
\r
33 has not provided a seed to either the RedRand32() or RedRand64() functions.
\r
35 static uint64_t ullGlobalRandomNumberSeed;
\r
37 /* Whether the above seed has been initialized.
\r
39 static bool fGlobalSeedInited;
\r
42 /** @brief Set the global seed used by the random number generator.
\r
44 The global seed gets used when RedRand64() or RedRand32() are called with
\r
45 a NULL seed argument.
\r
47 @param ullSeed The value to use as the global RNG seed.
\r
52 ullGlobalRandomNumberSeed = ullSeed;
\r
53 fGlobalSeedInited = true;
\r
57 /** @brief Generate a 64-bit pseudo-random number.
\r
59 The period of this random number generator is 2^64 (1.8 x 1019). These
\r
60 parameters are the same as the default one-stream SPRNG lcg64 generator and
\r
61 it satisfies the requirements for a maximal period.
\r
63 The tempering value is used and an AND mask and is specifically selected to
\r
64 favor the distribution of lower bits.
\r
66 @param pullSeed A pointer to the seed to use. Set this value to NULL to
\r
67 use the internal global seed value.
\r
69 @return A pseudo-random number in the range [0, UINT64_MAX].
\r
74 const uint64_t ullA = UINT64_SUFFIX(2862933555777941757);
\r
75 const uint64_t ullC = UINT64_SUFFIX(3037000493);
\r
76 const uint64_t ullT = UINT64_SUFFIX(4921441182957829599);
\r
78 uint64_t *pullSeedPtr;
\r
79 uint64_t ullLocalSeed;
\r
81 if(pullSeed != NULL)
\r
83 ullLocalSeed = *pullSeed;
\r
84 pullSeedPtr = pullSeed;
\r
88 if(!fGlobalSeedInited)
\r
90 /* Unfortunately, the Reliance Edge OS services don't give us much
\r
91 to work with to initialize the global seed. There is no entropy
\r
92 abstraction, no tick count abstraction, and the timestamp
\r
93 abstraction uses an opaque type which is not guaranteed to be an
\r
94 integer. The best we can do is use the RTC.
\r
96 Tests using the RNG should be supplying a seed anyway, for
\r
99 RedRandSeed((uint64_t)RedOsClockGetTime());
\r
102 ullLocalSeed = ullGlobalRandomNumberSeed;
\r
103 pullSeedPtr = &ullGlobalRandomNumberSeed;
\r
106 ullN = (ullLocalSeed * ullA) + ullC;
\r
108 *pullSeedPtr = ullN;
\r
110 /* The linear congruential generator used above produces good psuedo-random
\r
111 64-bit number sequences, however, as with any LCG, the period of the
\r
112 lower order bits is much shorter resulting in alternately odd/even pairs
\r
115 The result of the LGC above is tempered below with a series of XOR and
\r
116 shift operations to produce a more acceptable equidistribution of bits
\r
117 throughout the 64-bit range.
\r
119 ullN ^= (ullN >> 21U) & ullT;
\r
120 ullN ^= (ullN >> 43U) & ullT;
\r
121 ullN ^= (ullN << 23U) & ~ullT;
\r
122 ullN ^= (ullN << 31U) & ~ullT;
\r
128 /** @brief Generate a 32-bit pseudo-random number.
\r
130 @note The 32-bit random number generator internally uses the 64-bit random
\r
131 number generator, returning the low 32-bits of the pseudo-random
\r
134 @param pulSeed A pointer to the seed to use. Set this value to NULL to use
\r
135 the internal global seed value.
\r
137 @return A pseudo-random number in the range [0, UINT32_MAX].
\r
139 uint32_t RedRand32(
\r
144 if(pulSeed != NULL)
\r
146 uint64_t ullLocalSeed;
\r
148 ullLocalSeed = *pulSeed;
\r
149 ullN = RedRand64(&ullLocalSeed);
\r
150 *pulSeed = (uint32_t)ullLocalSeed;
\r
154 ullN = RedRand64(NULL);
\r
157 return (uint32_t)ullN;
\r