Commit | Line | Data |
---|---|---|
fc3e88fd KB |
1 | /* |
2 | * Copyright (c) 1988 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Timothy C. Stoehr. | |
7 | * | |
8 | * Redistribution and use in source and binary forms are permitted | |
9 | * provided that the above copyright notice and this paragraph are | |
10 | * duplicated in all such forms and that any documentation, | |
11 | * advertising materials, and other materials related to such | |
12 | * distribution and use acknowledge that the software was developed | |
13 | * by the University of California, Berkeley. The name of the | |
14 | * University may not be used to endorse or promote products derived | |
15 | * from this software without specific prior written permission. | |
16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
17 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
19 | */ | |
20 | ||
b3afadef | 21 | #ifndef lint |
fc3e88fd | 22 | static char sccsid[] = "@(#)random.c 5.2 (Berkeley) %G%"; |
b3afadef KB |
23 | #endif /* not lint */ |
24 | ||
fc3e88fd KB |
25 | /* |
26 | * random.c | |
27 | * | |
28 | * This source herein may be modified and/or distributed by anybody who | |
29 | * so desires, with the following restrictions: | |
30 | * 1.) No portion of this notice shall be removed. | |
31 | * 2.) Credit shall not be taken for the creation of this source. | |
32 | * 3.) This code is not to be traded, sold, or used for personal | |
33 | * gain or profit. | |
34 | * | |
35 | */ | |
36 | ||
b3afadef KB |
37 | static long rntb[32] = { |
38 | 3, 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, | |
39 | 0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb, 0x7449e56b, | |
40 | 0xbeb1dbb0, 0xab5c5918, 0x946554fd, 0x8c2e680f, 0xeb3d799f, | |
41 | 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, 0xe369735d, | |
42 | 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, | |
43 | 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, | |
44 | 0x8999220b, 0x27fb47b9 | |
45 | }; | |
46 | ||
47 | static long *fptr = &rntb[4]; | |
48 | static long *rptr = &rntb[1]; | |
49 | static long *state = &rntb[1]; | |
50 | static int rand_type = 3; | |
51 | static int rand_deg = 31; | |
52 | static int rand_sep = 3; | |
53 | static long *end_ptr = &rntb[32]; | |
54 | ||
55 | srrandom(x) | |
56 | int x; | |
57 | { | |
58 | register int i; | |
59 | long rrandom(); | |
60 | ||
61 | state[0] = (long) x; | |
62 | if (rand_type != 0) { | |
63 | for (i = 1; i < rand_deg; i++) { | |
64 | state[i] = 1103515245 * state[i - 1] + 12345; | |
65 | } | |
66 | fptr = &state[rand_sep]; | |
67 | rptr = &state[0]; | |
68 | for (i = 0; i < 10 * rand_deg; i++) { | |
69 | (void) rrandom(); | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
74 | long | |
75 | rrandom() | |
76 | { | |
77 | long i; | |
78 | ||
79 | if (rand_type == 0) { | |
80 | i = state[0] = (state[0]*1103515245 + 12345) & 0x7fffffff; | |
81 | } else { | |
82 | *fptr += *rptr; | |
83 | i = (*fptr >> 1) & 0x7fffffff; | |
84 | if (++fptr >= end_ptr) { | |
85 | fptr = state; | |
86 | ++rptr; | |
87 | } else { | |
88 | if (++rptr >= end_ptr) { | |
89 | rptr = state; | |
90 | } | |
91 | } | |
92 | } | |
93 | return(i); | |
94 | } | |
95 | ||
96 | get_rand(x, y) | |
97 | register int x, y; | |
98 | { | |
99 | register int r, t; | |
100 | long lr; | |
101 | ||
102 | if (x > y) { | |
103 | t = y; | |
104 | y = x; | |
105 | x = t; | |
106 | } | |
107 | lr = rrandom(); | |
108 | lr &= (long) 0x00003fff; | |
109 | r = (int) lr; | |
110 | r = (r % ((y - x) + 1)) + x; | |
111 | return(r); | |
112 | } | |
113 | ||
114 | rand_percent(percentage) | |
115 | register int percentage; | |
116 | { | |
117 | return(get_rand(1, 100) <= percentage); | |
118 | } | |
119 | ||
120 | coin_toss() | |
121 | { | |
122 | ||
123 | return(((rrandom() & 01) ? 1 : 0)); | |
124 | } |