Added IsSupportingRTP function to simplify detecting when STUN supports RTP
[pwlib.git] / src / ptclib / random.cxx
blobbc61715e757e5106a68651958662037d715e67e2
1 /*
2 * random.cxx
4 * ISAAC random number generator by Bob Jenkins.
6 * Portable Windows Library
8 * Copyright (c) 1993-2001 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * Based on code originally by Bob Jenkins.
28 * $Log$
29 * Revision 1.5 2003/02/20 23:32:00 robertj
30 * More RTEMS support patches, thanks Sebastian Meyer.
32 * Revision 1.4 2001/03/03 05:12:47 robertj
33 * Fixed yet another transcription error of random number generator code.
35 * Revision 1.3 2001/02/28 04:27:35 robertj
36 * Fixed stupid error in random number seeding, infinite loop.
38 * Revision 1.2 2001/02/27 03:33:44 robertj
39 * Changed random number generator due to licensing issues.
41 * Revision 1.1 2000/02/17 12:05:02 robertj
42 * Added better random number generator after finding major flaws in MSVCRT version.
47 #ifdef __GNUC__
48 #pragma implementation "random.h"
49 #endif
51 #include <ptlib.h>
52 #include <ptclib/random.h>
56 ///////////////////////////////////////////////////////////////////////////////
57 // PRandom
59 PRandom::PRandom()
61 SetSeed((DWORD)(time(NULL)+clock()));
65 PRandom::PRandom(DWORD seed)
67 SetSeed(seed);
71 #define mix(a,b,c,d,e,f,g,h) \
72 { \
73 a^=b<<11; d+=a; b+=c; \
74 b^=c>>2; e+=b; c+=d; \
75 c^=d<<8; f+=c; d+=e; \
76 d^=e>>16; g+=d; e+=f; \
77 e^=f<<10; h+=e; f+=g; \
78 f^=g>>4; a+=f; g+=h; \
79 g^=h<<8; b+=g; h+=a; \
80 h^=a>>9; c+=h; a+=b; \
84 void PRandom::SetSeed(DWORD seed)
86 int i;
87 DWORD a,b,c,d,e,f,g,h;
88 DWORD *m,*r;
89 randa = randb = randc = 0;
90 m=randmem;
91 r=randrsl;
93 for (i=0; i<RandSize; i++)
94 r[i] = seed++;
96 a=b=c=d=e=f=g=h=0x9e3779b9; /* the golden ratio */
98 for (i=0; i<4; ++i) /* scramble it */
100 mix(a,b,c,d,e,f,g,h);
103 /* initialize using the the seed */
104 for (i=0; i<RandSize; i+=8)
106 a+=r[i ]; b+=r[i+1]; c+=r[i+2]; d+=r[i+3];
107 e+=r[i+4]; f+=r[i+5]; g+=r[i+6]; h+=r[i+7];
108 mix(a,b,c,d,e,f,g,h);
109 m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
110 m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
113 /* do a second pass to make all of the seed affect all of m */
114 for (i=0; i<RandSize; i+=8)
116 a+=m[i ]; b+=m[i+1]; c+=m[i+2]; d+=m[i+3];
117 e+=m[i+4]; f+=m[i+5]; g+=m[i+6]; h+=m[i+7];
118 mix(a,b,c,d,e,f,g,h);
119 m[i ]=a; m[i+1]=b; m[i+2]=c; m[i+3]=d;
120 m[i+4]=e; m[i+5]=f; m[i+6]=g; m[i+7]=h;
123 randcnt=0;
124 Generate(); /* fill in the first set of results */
125 randcnt=RandSize; /* prepare to use the first set of results */
129 #define ind(mm,x) (*(DWORD *)((BYTE *)(mm) + ((x) & ((RandSize-1)<<2))))
131 #define rngstep(mix,a,b,mm,m,m2,r,x) \
133 x = *m; \
134 a = (a^(mix)) + *(m2++); \
135 *(m++) = y = ind(mm,x) + a + b; \
136 *(r++) = b = ind(mm,y>>RandBits) + x; \
139 unsigned PRandom::Generate()
141 if (randcnt--)
142 return randrsl[randcnt];
144 register DWORD a,b,x,y,*m,*mm,*m2,*r,*mend;
145 mm=randmem; r=randrsl;
146 a = randa; b = randb + (++randc);
147 for (m = mm, mend = m2 = m+(RandSize/2); m<mend; )
149 rngstep( a<<13, a, b, mm, m, m2, r, x);
150 rngstep( a>>6 , a, b, mm, m, m2, r, x);
151 rngstep( a<<2 , a, b, mm, m, m2, r, x);
152 rngstep( a>>16, a, b, mm, m, m2, r, x);
154 for (m2 = mm; m2<mend; )
156 rngstep( a<<13, a, b, mm, m, m2, r, x);
157 rngstep( a>>6 , a, b, mm, m, m2, r, x);
158 rngstep( a<<2 , a, b, mm, m, m2, r, x);
159 rngstep( a>>16, a, b, mm, m, m2, r, x);
161 randb = b; randa = a;
163 randcnt = RandSize-1;
164 return randrsl[randcnt];
168 #ifdef P_RTEMS
169 static PMutex mutex;
170 #endif
172 unsigned PRandom::Number()
174 #ifndef P_RTEMS
175 static PMutex mutex;
176 #endif
177 PWaitAndSignal wait(mutex);
179 static PRandom rand;
180 return rand;
184 // End Of File ///////////////////////////////////////////////////////////////