Leaked source code of windows server 2003
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
/*
* FILE: RandGen.h * * PURPOSE: Header file for a random number generator, based on * original code by George Marsaglia. * * AUTHOR: Karl Denninghoff, November 1998 * * Copyright: Microsoft Corporation, 1998 */
#ifndef RANDGEN_H
#define RANDGEN_H
#ifndef _BASETSD_H_ // if the base sized types not defined (header file not included)
typedef unsigned __int32 UINT32 ; #endif
typedef unsigned __int16 UINT16 ;
// maximum number returned by RandInt()
const UINT32 MAX_RANDGEN = 0xffffffffL;
//-------------------------------------------------------------
//
// CLASS CSARandGen
//
// PURPOSE: Generates random numbers of 32 bits with a period of
// about 2^250. However, since the seed is 32 bits, there
// are only 2^32 different possible sequences accessible
// through the interface provided.
//
// USAGE RULES and REMARKS:
// 1) You may have as many instances (random number generators)
// as you wish simultneously in a process.
// 2) The code is not thread-safe, i.e., you must not make
// simultaneous calls to a particular random number generator
// (CSARandGen instance) on different threads. If more than
// one thread is to call a particular generator, you should
// provide the synchronization necessary in some wrapper class.
// 3) Generation and subsequent conversion to a random real
// uniformly distributed over the interval [0,1] is provided
// by the RandReal() member function.
// 4) If you do not explicitly seed the generator, then the sequence
// generated will with high probability be indestinguishable from
// sequences generated by another instance. It will be an
// unrelated sequence that you will not be able to re-generate.
// 5) The constructor seeds the generator automatically using
// system clock input. However, it does not tell you this value.
// If you want a random seed (one you likely have not used before) and
// you also want to be able to repeat the sequence later, then
// the following technique will work well.
//
// CSARandGen *pRandGen = new CSARandGen;
// UINT32 ulSeed = pRandGen->RandInt(); // gets a random seed value
// pRandGen->RandSeed( ulSeed );
// // now save ulSeed so you can use it again to seed
// // a generator. Any CSARandGen instance will produce
// // the same sequence after calling the seed function
// // with the same seed value.
//
//
class CSARandGen { public: // public functions
_stdcall CSARandGen();
// seeds the random number generator, can be called at
// any time to re-seed, seeding with the same number starts
// the same sequence.
void _stdcall RandSeed( UINT32 ulSeed );
// returns uniformly distributed random number
// between 0 and MAX_RANDGEN inclusive, period about 2^250
UINT32 _stdcall RandInt();
// returns uniformly distributed random floating point number
// between 0 and 1. Two adjacent possible numbers are
// 1/MAX_RANDGEN apart.
double _stdcall RandReal() { return double(RandInt())/double(MAX_RANDGEN); };
private: // private data members
UINT16 mother1[10]; UINT16 mother2[10]; static LONG slCInstances; // keeps count of instantiations, used to ensure
// uniqueness of default seeds when two instantiations
// result in the same tick and filetime.
};
#endif
|