1 /*-------------------------------------------------------------------------
2 * drawElements Base Portability Library
3 * -------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Random number generation.
22 *//*--------------------------------------------------------------------*/
31 /*--------------------------------------------------------------------*//*!
32 * \brief Initialize a random number generator with a given seed.
33 * \param rnd RNG to initialize.
34 * \param seed Seed value used for random values.
35 *//*--------------------------------------------------------------------*/
36 void deRandom_init (deRandom* rnd, deUint32 seed)
38 rnd->x = (deUint32)(-(int)seed ^ 123456789);
39 rnd->y = (deUint32)(362436069 * seed);
40 rnd->z = (deUint32)(521288629 ^ (seed >> 7));
41 rnd->w = (deUint32)(88675123 ^ (seed << 3));
44 /*--------------------------------------------------------------------*//*!
45 * \brief Get a pseudo random uint32.
46 * \param rnd Pointer to RNG.
47 * \return Random uint32 number.
48 *//*--------------------------------------------------------------------*/
49 deUint32 deRandom_getUint32 (deRandom* rnd)
54 t = rnd->x ^ (rnd->x << 11);
58 rnd->w = w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
62 /*--------------------------------------------------------------------*//*!
63 * \brief Get a pseudo random uint64.
64 * \param rnd Pointer to RNG.
65 * \return Random uint64 number.
66 *//*--------------------------------------------------------------------*/
67 deUint64 deRandom_getUint64 (deRandom* rnd)
69 deUint64 x = deRandom_getUint32(rnd);
70 return x << 32 | deRandom_getUint32(rnd);
73 /*--------------------------------------------------------------------*//*!
74 * \brief Get a pseudo random float in range [0, 1[.
75 * \param rnd Pointer to RNG.
76 * \return Random float number.
77 *//*--------------------------------------------------------------------*/
78 float deRandom_getFloat (deRandom* rnd)
80 return (float)(deRandom_getUint32(rnd) & 0xFFFFFFFu) / (float)(0xFFFFFFFu+1);
83 /*--------------------------------------------------------------------*//*!
84 * \brief Get a pseudo random float in range [0, 1[.
85 * \param rnd Pointer to RNG.
86 * \return Random float number.
87 *//*--------------------------------------------------------------------*/
88 double deRandom_getDouble (deRandom* rnd)
90 DE_STATIC_ASSERT(FLT_RADIX == 2);
91 return ldexp((double)(deRandom_getUint64(rnd) & ((1ull << DBL_MANT_DIG) - 1)),
95 /*--------------------------------------------------------------------*//*!
96 * \brief Get a pseudo random boolean value (DE_FALSE or DE_TRUE).
97 * \param rnd Pointer to RNG.
98 * \return Random float number.
99 *//*--------------------------------------------------------------------*/
100 deBool deRandom_getBool (deRandom* rnd)
102 deUint32 val = deRandom_getUint32(rnd);
103 return ((val & 0xFFFFFF) < 0x800000);