-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRANDOM.CPP
88 lines (77 loc) · 3.9 KB
/
RANDOM.CPP
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
/******************************************************************************/
/* */
/* RANDOM - Generate 32 bit period-free random numbers. */
/* */
/* void pf_srand ( long iseed ) - Set the random seed */
/* long pf_rand () - Return a 32 bit random integer in range 0-259199 */
/* long pf_randmax () - Return the maximum number which pf_rand may return */
/* */
/* Copyright (c) 1993 by Academic Press, Inc. */
/* */
/* All rights reserved. Permission is hereby granted, until further notice, */
/* to make copies of this diskette, which are not for resale, provided these */
/* copies are made from this master diskette only, and provided that the */
/* following copyright notice appears on the diskette label: */
/* (c) 1993 by Academic Press, Inc. */
/* */
/* Except as previously stated, no part of the computer program embodied in */
/* this diskette may be reproduced or transmitted in any form or by any means,*/
/* electronic or mechanical, including input into storage in any information */
/* system for resale, without permission in writing from the publisher. */
/* */
/* Produced in the United States of America. */
/* */
/* ISBN 0-12-479041-0 */
/* */
/******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <ctype.h>
#include <stdlib.h>
#define IM 714025L // These constants are from Press et. al.
#define IA 1366L // "Numerical Recipes in C"
#define IC 150889L // Do not tamper with them unless you are an expert
#define TABLE_LENGTH 97 // Length of shuffle table
static long seed = 797 ; // Keep the current seed here
static long randout ; // Random output
static long table[TABLE_LENGTH] ; // Keep shuffle table here
static int table_initialized = 0 ; // Has it been initialized?
/*
Set the random seed
*/
void pf_srand ( long iseed )
{
seed = iseed ; // We keep the static seed here
table_initialized = 0 ; // Must also rebuild table with it!
}
/*
Return the maximum random number
*/
long pf_randmax ()
{
return IM - 1L ;
}
/*
This is the actual random number generator
*/
long pf_rand () // Return the next random number
{
int i ;
if (! table_initialized) { // Initialize shuffle table before use
table_initialized = 1 ; // Flag to avoid more inits
for (i=0 ; i<TABLE_LENGTH ; i++) { // Fill entire table
seed = (IA * seed + IC) % IM ; // Make a random number
table[i] = seed ; // Put it in the table
}
seed = (IA * seed + IC) % IM ; // One more random number
randout = seed ; // for upcoming first use
}
i = (int) ((double) TABLE_LENGTH * (double) randout / (double) IM) ;
randout = table[i] ; // This output comes from table
seed = (IA * seed + IC) % IM ; // Make new random number
table[i] = seed ; // to replace used entry
return randout ; // then return old entry
}