VirtualBox

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

最後變更 在這個檔案從71997是 62812,由 vboxsync 提交於 8 年 前

GuestHost/OpenGL: warnings

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 2.9 KB
 
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# ifdef VBOX
12# include <iprt/win/windows.h>
13# else
14#include <windows.h>
15# endif
16#else
17#include <sys/time.h>
18#endif
19
20
21/* Period parameters */
22#define N 624
23#define M 397
24#define MATRIX_A 0x9908b0df /* constant vector a */
25#define UPPER_MASK 0x80000000 /* most significant w-r bits */
26#define LOWER_MASK 0x7fffffff /* least significant r bits */
27
28/* Tempering parameters */
29#define TEMPERING_MASK_B 0x9d2c5680
30#define TEMPERING_MASK_C 0xefc60000
31#define TEMPERING_SHIFT_U(y) (y >> 11)
32#define TEMPERING_SHIFT_S(y) (y << 7)
33#define TEMPERING_SHIFT_T(y) (y << 15)
34#define TEMPERING_SHIFT_L(y) (y >> 18)
35
36static unsigned long mt[N]; /* the array for the state vector */
37static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
38
39void crRandSeed(unsigned long seed)
40{
41 /* setting initial seeds to mt[N] using the generator Line 25 of Table 1
42 in [KNUTH 1981, The Art of Computer Programming Vol. 2 (2nd Ed.),
43 pp102
44 */
45 if (seed == 0)
46 seed = 4357; /* pick default seed if seed=0 (Guy Zadickario) */
47 mt[0]= seed & 0xffffffff;
48 for (mti=1; mti<N; mti++)
49 mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
50}
51
52
53/*
54 * Seed the generator based on time of day or a system counter.
55 */
56void crRandAutoSeed(void)
57{
58#if defined(WINDOWS)
59 LARGE_INTEGER counter;
60 QueryPerformanceCounter( &counter );
61 crRandSeed((unsigned long) counter.QuadPart);
62#else
63 struct timeval timeofday;
64 gettimeofday( &timeofday, 0 );
65 crRandSeed((unsigned long) timeofday.tv_usec);
66#endif
67}
68
69
70static double genrand( void )
71{
72 unsigned long y;
73 static unsigned long mag01[2]={0x0, MATRIX_A};
74 /* mag01[x] = x * MATRIX_A for x=0,1 */
75
76 if (mti >= N) { /* generate N words at one time */
77 int kk;
78 if (mti == N+1) /* if sgenrand() has not been called, */
79 crRandSeed(4357); /* a default initial seed is used */
80
81 for (kk=0;kk<N-M;kk++) {
82 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
83 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
84 }
85 for (;kk<N-1;kk++) {
86 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
87 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
88 }
89 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
90 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
91 mti = 0;
92 }
93
94 y = mt[mti++];
95 y ^= TEMPERING_SHIFT_U(y);
96 y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
97 y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
98 y ^= TEMPERING_SHIFT_L(y);
99
100 return ( (double)y / (unsigned long)0xffffffff ); /* reals */
101 /* return y; */ /* for integer generation */
102}
103
104float crRandFloat(float min, float max)
105{
106 double t = genrand();
107 return (float) (t*min + (1-t)*max);
108}
109
110/* return a random integer in [min, max] (inclusive). */
111int crRandInt(int min, int max)
112{
113 double t = genrand();
114 return min + (int) (t * (max - min + 1));
115}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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