VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/rand.c@ 53426

Last change on this file since 53426 was 15532, checked in by vboxsync, 16 years ago

crOpenGL: export to OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_rand.h"
8
9#ifdef WINDOWS
10#define WIN32_LEAN_AND_MEAN
11#include <windows.h>
12#else
13#include <sys/time.h>
14#endif
15
16
17/* Period parameters */
18#define N 624
19#define M 397
20#define MATRIX_A 0x9908b0df /* constant vector a */
21#define UPPER_MASK 0x80000000 /* most significant w-r bits */
22#define LOWER_MASK 0x7fffffff /* least significant r bits */
23
24/* Tempering parameters */
25#define TEMPERING_MASK_B 0x9d2c5680
26#define TEMPERING_MASK_C 0xefc60000
27#define TEMPERING_SHIFT_U(y) (y >> 11)
28#define TEMPERING_SHIFT_S(y) (y << 7)
29#define TEMPERING_SHIFT_T(y) (y << 15)
30#define TEMPERING_SHIFT_L(y) (y >> 18)
31
32static unsigned long mt[N]; /* the array for the state vector */
33static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
34
35void crRandSeed(unsigned long seed)
36{
37 /* setting initial seeds to mt[N] using the generator Line 25 of Table 1
38 in [KNUTH 1981, The Art of Computer Programming Vol. 2 (2nd Ed.),
39 pp102
40 */
41 if (seed == 0)
42 seed = 4357; /* pick default seed if seed=0 (Guy Zadickario) */
43 mt[0]= seed & 0xffffffff;
44 for (mti=1; mti<N; mti++)
45 mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
46}
47
48
49/*
50 * Seed the generator based on time of day or a system counter.
51 */
52void crRandAutoSeed(void)
53{
54#if defined(WINDOWS)
55 LARGE_INTEGER counter;
56 QueryPerformanceCounter( &counter );
57 crRandSeed((unsigned long) counter.QuadPart);
58#else
59 struct timeval timeofday;
60 gettimeofday( &timeofday, 0 );
61 crRandSeed((unsigned long) timeofday.tv_usec);
62#endif
63}
64
65
66static double genrand( void )
67{
68 unsigned long y;
69 static unsigned long mag01[2]={0x0, MATRIX_A};
70 /* mag01[x] = x * MATRIX_A for x=0,1 */
71
72 if (mti >= N) { /* generate N words at one time */
73 int kk;
74 if (mti == N+1) /* if sgenrand() has not been called, */
75 crRandSeed(4357); /* a default initial seed is used */
76
77 for (kk=0;kk<N-M;kk++) {
78 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
79 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
80 }
81 for (;kk<N-1;kk++) {
82 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
83 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
84 }
85 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
86 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
87 mti = 0;
88 }
89
90 y = mt[mti++];
91 y ^= TEMPERING_SHIFT_U(y);
92 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
93 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
94 y ^= TEMPERING_SHIFT_L(y);
95
96 return ( (double)y / (unsigned long)0xffffffff ); /* reals */
97 /* return y; */ /* for integer generation */
98}
99
100float crRandFloat(float min, float max)
101{
102 double t = genrand();
103 return (float) (t*min + (1-t)*max);
104}
105
106/* return a random integer in [min, max] (inclusive). */
107int crRandInt(int min, int max)
108{
109 double t = genrand();
110 return min + (int) (t * (max - min + 1));
111}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette