1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <math.h>
//double randgen(unsigned long ran[3])
double randgen(void);/* This generates a random number uniformly distributed between 0 and 1 */
int randint(int lo, int hi); /* This generates a random integer uniformly distributed among
the set { lo, lo+1,...., hi-1,hi} */
/* This line initialises the seeds of randgen (it requires three). Change one or more of
the three seed integers to get a new random number sequence.
This line should appear only once in your code. */
static unsigned long ranx=1,rany=1,ranz=1;
/* Randgen returns a double value uniformly distributed between 0 and 1. It uses three integer
seeds (ranx, rany and ranz) */
void getRanx(unsigned long ran[3])
{
ran[0]=ranx;
ran[1]=rany;
ran[2]=ranz;
}
double randgen(void)
{
double register random;
ranx = (171*ranx)%30269;
rany = (172*rany)%30307;
ranz = (170*ranz)%30323;
random = ranx/30269.0+rany/30307.0+ranz/30323.0;
// Discard value (max = 2) to left of decimal point
while(random>1.0)
random -= 1.0;
return random;
} |
Partager