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