summaryrefslogtreecommitdiff
path: root/randnum.c
blob: 21189b5340bb52214077465d8e8a0347f45185df (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdlib.h>

#include "cleanbench.h"

/****************************
** RANDOM NUMBER GENERATOR **
*****************************
** This is a second-order linear congruential random number
** generator.  Its advantage is (of course) that it can be
** seeded and will thus produce repeatable sequences of
** random numbers.
*/

/****************************
*        randnum()          *
*****************************
** Second order linear congruential generator.
** Constants suggested by J. G. Skellam.
** If val==0, returns next member of sequence.
**    val!=0, restart generator.
*/

int randnum(int val) {
	static int randw[2] = { 13 , 117 };
	int interm;

	if (val != 0) {
		randw[0] = 13;
		randw[1] = 117;
	}

	interm = (randw[0] * (int)254754 + randw[1] * (int)529562) % (int)999563;
	randw[1] = randw[0];
	randw[0] = interm;
	return(interm);
}

/****************************
*         randwc()          *
*****************************
** Returns signed 32-bit random modulo num.
*/
int randwc(int num) {
	return(randnum(0) % num);
}

/***************************
**      abs_randwc()      **
****************************
** Same as randwc(), only this routine returns only
** positive numbers.
*/
int abs_randwc(int num) {
	return abs(randwc(num));
}