forked from RiverWalter/AlgorithmCodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA09.00.cpp
More file actions
27 lines (27 loc) · 708 Bytes
/
Copy pathA09.00.cpp
File metadata and controls
27 lines (27 loc) · 708 Bytes
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
//Algorithm 9.0 (P267)(A09.00.cpp)
//random number generator by Linear Congruential Method
//线性同余法产生伪随机数
#include "headers.h"
#define MULTIPLIER 0x015A4E35L
#define INCREMENT 1
static unsigned long seed;
void random_seed9(unsigned long d)
{
if (d==0)
seed = (unsigned long)time(0);
else
seed = d;
}
unsigned int random9(unsigned long low,unsigned long high)
{
seed = MULTIPLIER * seed + INCREMENT;
return ((seed >> 16) % (high - low) + low);
}
void random9Test(int n, int low, int high)
{
random_seed9(0);
printf("线性同余法(random9函数)产生的随机数。\n");
printf("范围在[%d,%d]间的%d个随机数\n", low, high, n);
for (int i=0; i<n; i++)
printf("%3d:%d\n",i+1, random9(low, high+1));
}