-
Notifications
You must be signed in to change notification settings - Fork 1
/
rand.c
73 lines (59 loc) · 1.37 KB
/
rand.c
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
/* Sarien - A Sierra AGI resource interpreter engine
* Copyright (C) 1999-2001 Stuart George and Claudio Matsuoka
*
* $Id: rand.c,v 1.9 2002/03/31 18:46:24 cmatsuoka Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; see docs/COPYING for further details.
*/
#ifndef PALMOS
#include <time.h>
#else
#define time(x) (x)
#endif
#include "sarien.h"
#include "rand.h"
#define RNG_M 2147483647L
#define RNG_A 48271L
#define RNG_Q 127773L
#define RNG_R 2836L
SINT32 rnd_seed;
static void set_xrnd_seed(SINT32 seedval)
{
rnd_seed = (seedval % (RNG_M-1)) + 1;
}
static SINT32 xrnd(void)
{
SINT32 low, high, test;
high = rnd_seed / RNG_Q;
low = rnd_seed % RNG_Q;
test = RNG_A * low - RNG_R * high;
return rnd_seed = test > 0 ? test : test + RNG_M;
}
/*
* Public functions
*/
/**
* Set the random number generator seed.
*/
void set_rnd_seed ()
{
set_xrnd_seed(time(NULL));
}
/**
* Read the random number generator seed.
*/
SINT32 get_rnd_seed ()
{
return rnd_seed;
}
/**
* Return random number.
* This function returns a random value lesser than the specified value.
*/
SINT32 rnd (SINT32 maxrnd)
{
return maxrnd ? xrnd() % maxrnd : xrnd();
}
/* end: rand.c */