📄 random.h
字号:
#include <math.h>
#define max_int 32767
class Random
{
public:
Random(bool pseudo=true);
// Declare random-number generation methods here.
double random_real( );
int random_integer(int low, int high);
int poisson(double mean);
private:
int reseed( ); //Re-randomize theseed .
int seed,multiplier, add_on; // constants for use in arithmetic operations
};
Random::Random(bool pseudo)
// Post: The values ofseed ,add on , andmultiplier are initialized. Theseed
// is initialized randomly only ifpseudo == false.
{
if(pseudo)seed=1;
else seed=time(NULL)% max_int;
multiplier = 2743;
add_on = 5923;
}
int Random::reseed( )
// Post: Theseed is replaced by a psuedorandom successor.
{ seed=seed*multiplier+add_on;
return seed;
}
double Random::random_real( )
/* Post: A random real_number between 0 and 1 is returned. */
{
double max = max_int+1.0;
double temp = reseed( );
if (temp<0) temp=temp+max;
return temp/max;
}
int Random::random_integer(int low, int high)
/* Post: A random integer betweenlow andhigh (inclusive) is returned. */
{
if(low>high) return random_integer(high,low);
else return ((int) ((high-low+1)*random_real( )))+low;
}
int Random::poisson(double mean)
/* Post: A random integer, reecting a Poisson distribution with parametermean ,
is returned. */
{
double limit = exp(-mean);
double product=random_real( );
int count = 0;
while(product>limit)
{ count++;
product *= random_real( );
}
return count;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -