Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:21:08

0001 #ifndef __SIMPLERANDOM_H__
0002 #define __SIMPLERANDOM_H__
0003 
0004 /*
0005  *  After becoming frustrated with the lack of a standalone, portable,
0006  *  decent random number generator, I decided to make one based on a
0007  *  cryptographic one-way hash function.  I chose MD5 since it is fast
0008  *  and free source was readily available.  More cryptographically
0009  *  secure hash functions are available (e.g. SHA-1), but for the
0010  *  purposes of a rand/random/erand48 replacement, MD5 should be more
0011  *  than sufficient.
0012  *
0013  *  MD5 takes an arbitrary amount of input and yields a 16 byte hash.
0014  *  This RNG continually MD5's a 16 byte digest, and uses the bottom N
0015  *  bits as the random number yielded, where N is just large enough to
0016  *  include the largest random number desired.
0017  *
0018  *      To yield a random number between 0 and r:
0019  *
0020  *              create mask which has enough bits to include all of r
0021  *                      (for example, if r is 100, mask would be 0x7F)
0022  *
0023  *              do {
0024  *                      digest = MD5(digest)
0025  *                      number = digest & mask
0026  *              } while (number > r)
0027  *
0028  *  The digest should be loaded and saved to a disk file between
0029  *  invocations of a program using the RNG.
0030  *
0031  *  Random functions appear after the included MD5 code.
0032  *
0033  *  Send comments to:  skrenta@pbm.com (Rich Skrenta)
0034  */
0035 
0036 
0037 /*****************************************************************/
0038 
0039 /*
0040  * This code implements the MD5 message-digest algorithm.
0041  * The algorithm is due to Ron Rivest.  This code was
0042  * written by Colin Plumb in 1993, no copyright is claimed.
0043  * This code is in the public domain; do with it what you wish.
0044  *
0045  * Equivalent code is available from RSA Data Security, Inc.
0046  * This code has been tested against that, and is equivalent,
0047  * except that you don't need to include two pages of legalese
0048  * with every copy.
0049  *
0050  * To compute the message digest of a chunk of bytes, declare an
0051  * MD5Context structure, pass it to MD5Init, call MD5Update as
0052  * needed on buffers full of bytes, and then call MD5Final, which
0053  * will fill a supplied 16-byte array with the digest.
0054  */
0055 
0056 
0057 typedef unsigned int word32;
0058 typedef unsigned char byte;
0059 
0060 struct xMD5Context {
0061         word32 buf[4];
0062         word32 bytes[2];
0063         word32 in[16];
0064 };
0065 
0066 class simpleRandom {
0067 public:
0068   simpleRandom();
0069   simpleRandom(const int iseed);
0070   
0071   float gauss(const float mean, const float sigma);
0072   float rnd(int low, int high);
0073 
0074 private:
0075   
0076   void byteSwap(word32 *buf, unsigned words);
0077   void xMD5Init(struct xMD5Context *ctx);
0078   void xMD5Update(struct xMD5Context *ctx, byte const *buf, int len);
0079   void xMD5Final(byte digest[16], struct xMD5Context *ctx);
0080   void xMD5Transform(word32 buf[4], word32 const in[16]);
0081   void MD5(byte *dest, const byte *orig, int len);
0082   void load_seed();
0083 
0084   unsigned int digest[4];
0085     
0086 };
0087 #endif
0088