summaryrefslogtreecommitdiff
path: root/misc.c
blob: aca70bc4cc400406454c2816830666537896342a (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

/*
** misc.c
** BYTEmark (tm)
** BYTE's Native Mode Benchmarks
** Rick Grehan, BYTE Magazine
** DISCLAIMER
** The source, executable, and documentation files that comprise
** the BYTEmark benchmarks are made available on an "as is" basis.
** This means that we at BYTE Magazine have made every reasonable
** effort to verify that the there are no errors in the source and
** executable code.  We cannot, however, guarantee that the programs
** are error-free.  Consequently, McGraw-HIll and BYTE Magazine make
** no claims in regard to the fitness of the source code, executable
** code, and documentation of the BYTEmark.
**  Furthermore, BYTE Magazine, McGraw-Hill, and all employees
** of McGraw-Hill cannot be held responsible for any damages resulting
** from the use of this code or the results obtained from using
** this code.
*/

#include <stdio.h>
#include "misc.h"

/***********************************************************
**     MISCELLANEOUS BUT OTHERWISE NECESSARY ROUTINES     **
***********************************************************/

/****************************
** 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.
*/

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

/***************************
**      abs_randwc()      **
****************************
** Same as randwc(), only this routine returns only
** positive numbers.
*/
/*
unsigned long abs_randwc(unsigned long num)
{
long temp;

temp=randwc(num);
if(temp<0) temp=0L-temp;

return((unsigned long)temp);
}
*/
uint32_t abs_randwc(uint32_t num)
{
int32_t temp;		/* Temporary storage */ 

temp=randwc(num);
if(temp<0) temp=(int32_t)0-temp;

return((uint32_t)temp);
}

/****************************
*        randnum()          *
*****************************
** Second order linear congruential generator.
** Constants suggested by J. G. Skellam.
** If val==0, returns next member of sequence.
**    val!=0, restart generator.
*/
/*
long randnum(long lngval)
{
	register long interm;
	static long randw[2] = { 13L , 117L };

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

	interm=(randw[0]*254754L+randw[1]*529562L)%999563L;
	randw[1]=randw[0];
	randw[0]=interm;
	return(interm);
}
*/
int32_t randnum(int32_t lngval)
{
	register int32_t interm;
	static int32_t randw[2] = { (int32_t)13 , (int32_t)117 };

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

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