📄 random.cc
字号:
// ################################################################################
//
// name: random.cc
//
// author: Martin Pelikan
//
// purpose: random number generator related functions (random generator is
// based on the code by Fernando Lobo, Prime Modulus Multiplicative
// Linear Congruential Generator (PMMLCG)
//
// last modified: February 1999
//
// ################################################################################
#include <stdio.h>
#include "random.h"
#define M 2147483647 // modulus of PMMLCG (the default is 2147483647 = 2^31 - 1)
#define A 16807 // the default is 16807
static long Q = M/A; // M / A
static long R = M%A; // M mod A
static long seed; // a number between 1 and m-1
// ================================================================================
//
// name: drand
//
// function: returns a floating-point random number generated according to
// uniform distribution from [0,1)
//
// parameters: (none)
//
// returns: (double) resulting random number
//
// ================================================================================
double drand()
{
long lo,hi,test;
hi = seed / Q;
lo = seed % Q;
test = A*lo - R*hi;
if (test>0)
seed = test;
else
seed = test+M;
return double(seed)/M;
};
// ================================================================================
//
// name: setSeed
//
// function: sets the random seed
//
// parameters: seed.........a new random seed
//
// returns: (long) the result of the operation
//
// ================================================================================
long setSeed(long newSeed)
{
// set the seed and return the result of the operation
return (seed = newSeed);
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -