File indexing completed on 2025-08-02 08:21:08
0001 #include "simpleRandom.h"
0002 #include <math.h>
0003 #include <strings.h>
0004
0005
0006
0007
0008
0009
0010
0011 void
0012 simpleRandom::byteSwap(word32 *buf, unsigned words)
0013 {
0014 byte *p = (byte *)buf;
0015
0016 do {
0017 *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
0018 ((unsigned)p[1] << 8 | p[0]);
0019 p += 4;
0020 } while (--words);
0021 }
0022
0023
0024
0025
0026
0027 void
0028 simpleRandom::xMD5Init(struct xMD5Context *ctx)
0029 {
0030 ctx->buf[0] = 0x67452301;
0031 ctx->buf[1] = 0xefcdab89;
0032 ctx->buf[2] = 0x98badcfe;
0033 ctx->buf[3] = 0x10325476;
0034
0035 ctx->bytes[0] = 0;
0036 ctx->bytes[1] = 0;
0037 }
0038
0039
0040
0041
0042
0043 void
0044 simpleRandom::xMD5Update(struct xMD5Context *ctx, byte const *buf, int len)
0045 {
0046 word32 t;
0047
0048
0049
0050 t = ctx->bytes[0];
0051 if ((ctx->bytes[0] = t + len) < t)
0052 ctx->bytes[1]++;
0053
0054 t = 64 - (t & 0x3f);
0055 if (t > len) {
0056 bcopy(buf, (byte *)ctx->in + 64 - (unsigned)t, len);
0057 return;
0058 }
0059
0060 bcopy(buf,(byte *)ctx->in + 64 - (unsigned)t, (unsigned)t);
0061 byteSwap(ctx->in, 16);
0062 xMD5Transform(ctx->buf, ctx->in);
0063 buf += (unsigned)t;
0064 len -= (unsigned)t;
0065
0066
0067 while (len >= 64) {
0068 bcopy(buf, ctx->in, 64);
0069 byteSwap(ctx->in, 16);
0070 xMD5Transform(ctx->buf, ctx->in);
0071 buf += 64;
0072 len -= 64;
0073 }
0074
0075
0076 bcopy(buf, ctx->in, len);
0077 }
0078
0079
0080
0081
0082
0083 void
0084 simpleRandom::xMD5Final(byte bdigest[16], struct xMD5Context *ctx)
0085 {
0086 int count = (int)(ctx->bytes[0] & 0x3f);
0087 byte *p = (byte *)ctx->in + count;
0088
0089
0090 *p++ = 0x80;
0091
0092
0093 count = 56 - 1 - count;
0094
0095 if (count < 0) {
0096 bzero(p, count+8);
0097 byteSwap(ctx->in, 16);
0098 xMD5Transform(ctx->buf, ctx->in);
0099 p = (byte *)ctx->in;
0100 count = 56;
0101 }
0102 bzero(p, count+8);
0103 byteSwap(ctx->in, 14);
0104
0105
0106 ctx->in[14] = ctx->bytes[0] << 3;
0107 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
0108 xMD5Transform(ctx->buf, ctx->in);
0109
0110 byteSwap(ctx->buf, 4);
0111 bcopy(ctx->buf, bdigest, 16);
0112 bzero(ctx,sizeof(*ctx));
0113 }
0114
0115
0116
0117
0118
0119 #define F1(x, y, z) (z ^ (x & (y ^ z)))
0120 #define F2(x, y, z) F1(z, x, y)
0121 #define F3(x, y, z) (x ^ y ^ z)
0122 #define F4(x, y, z) (y ^ (x | ~z))
0123
0124
0125 #define MD5STEP(f,w,x,y,z,in,s) \
0126 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
0127
0128
0129
0130
0131
0132
0133 void
0134 simpleRandom::xMD5Transform(word32 buf[4], word32 const in[16])
0135 {
0136 word32 a, b, c, d;
0137
0138 a = buf[0];
0139 b = buf[1];
0140 c = buf[2];
0141 d = buf[3];
0142
0143 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
0144 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
0145 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
0146 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
0147 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
0148 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
0149 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
0150 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
0151 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
0152 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
0153 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
0154 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
0155 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
0156 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
0157 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
0158 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
0159
0160 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
0161 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
0162 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
0163 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
0164 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
0165 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
0166 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
0167 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
0168 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
0169 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
0170 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
0171 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
0172 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
0173 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
0174 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
0175 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
0176
0177 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
0178 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
0179 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
0180 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
0181 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
0182 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
0183 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
0184 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
0185 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
0186 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
0187 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
0188 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
0189 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
0190 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
0191 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
0192 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
0193
0194 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
0195 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
0196 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
0197 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
0198 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
0199 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
0200 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
0201 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
0202 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
0203 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
0204 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
0205 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
0206 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
0207 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
0208 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
0209 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
0210
0211 buf[0] += a;
0212 buf[1] += b;
0213 buf[2] += c;
0214 buf[3] += d;
0215 }
0216
0217
0218 void simpleRandom::MD5(byte *dest, const byte *orig, int len)
0219 {
0220 struct xMD5Context context;
0221
0222 xMD5Init(&context);
0223 xMD5Update(&context, orig, len);
0224 xMD5Final(dest, &context);
0225 }
0226
0227
0228
0229 simpleRandom::simpleRandom()
0230 {
0231 digest[0] = 7657635;
0232 digest[1] = 5565649;
0233 digest[2] = 9827729;
0234 digest[3] = 9892898;
0235
0236
0237 }
0238
0239 simpleRandom::simpleRandom( const int iseed)
0240 {
0241 digest[0] = iseed;
0242 digest[1] = 565649;
0243 digest[2] = 6827729;
0244 digest[3] = 2892898;
0245
0246 }
0247
0248 float simpleRandom::gauss(const float mean, const float sigma)
0249 {
0250 #define BIGNUMBER 100000
0251
0252 float y = 0.;
0253 while(y == 0.)
0254 {
0255 y = rnd(0,BIGNUMBER);
0256 }
0257 float z = rnd(0,BIGNUMBER);
0258
0259 y /= BIGNUMBER;
0260 z /= BIGNUMBER;
0261
0262 float x = z * 6.283185;
0263
0264 #if defined(SunOS) || defined(Linux)
0265 float result = mean + sigma* sin(x) * sqrt(-2 * log(y) );
0266 #else
0267 float result = mean + sigma* sinf(x) * sqrtf(-2 * logf(y) );
0268 #endif
0269
0270 return result;
0271 }
0272
0273 float simpleRandom::rnd(int low, int high)
0274 {
0275 unsigned int range = high - low;
0276 unsigned int mask = 0;
0277 unsigned int num;
0278 int r;
0279
0280 for (r = range; r; r >>= 1)
0281 mask |= r;
0282
0283 do {
0284 MD5((byte*)digest,(const byte *) digest, sizeof(digest));
0285 num = digest[0] & mask;
0286 } while (num > range);
0287
0288 return num + low;
0289 }