Lines Matching +full:state +full:- +full:of +full:- +full:the +full:- +full:art
1 /* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
30 * This file contains a simple, straightforward implementation of the Park
32 * multiplicative linear congruential generator (MLCG) with a period of
33 * 2^31-1.
41 * If initial seeds are not selected randomly, two instances of the PRNG
42 * can be correlated. [Knuth81, pp. 32-33] describes a shuffling technique
45 * If PRNGs are used for simulation, the period of the current
46 * implementation may be too short. [LE88] discusses methods of combining
49 * long-period PRNGs.
53 * [Knuth81] Donald E. Knuth. The Art of Computer Programming. Volume 2:
54 * Seminumerical Algorithms. Reading, Massachusetts: Addison-Wesley, 1981.
57 * Generators". CACM 31(6), June 1988, pp. 742-774.
60 * October 1990, pp. 85-97.
63 * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
65 * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
66 * CPUs". Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
70 * Number Generators". CACM 36(7), July 1993, pp. 105-110.
85 RandomState *state; in drmRandomCreate() local
87 state = drmMalloc(sizeof(*state)); in drmRandomCreate()
88 if (!state) return NULL; in drmRandomCreate()
89 state->magic = RANDOM_MAGIC; in drmRandomCreate()
92 state->a = 16807; in drmRandomCreate()
93 state->m = 2147483647; in drmRandomCreate()
94 state->check = 1043618065; /* After 10000 iterations */ in drmRandomCreate()
97 state->a = 48271; in drmRandomCreate()
98 state->m = 2147483647; in drmRandomCreate()
99 state->check = 399268537; /* After 10000 iterations */ in drmRandomCreate()
101 state->q = state->m / state->a; in drmRandomCreate()
102 state->r = state->m % state->a; in drmRandomCreate()
104 state->seed = seed; in drmRandomCreate()
107 if (state->seed <= 0) state->seed = 1; in drmRandomCreate()
108 if (state->seed >= state->m) state->seed = state->m - 1; in drmRandomCreate()
110 return state; in drmRandomCreate()
113 drm_public int drmRandomDestroy(void *state) in drmRandomDestroy() argument
115 drmFree(state); in drmRandomDestroy()
119 drm_public unsigned long drmRandom(void *state) in drmRandom() argument
121 RandomState *s = (RandomState *)state; in drmRandom()
125 hi = s->seed / s->q; in drmRandom()
126 lo = s->seed % s->q; in drmRandom()
127 s->seed = s->a * lo - s->r * hi; in drmRandom()
128 if ((s->a * lo) <= (s->r * hi)) s->seed += s->m; in drmRandom()
130 return s->seed; in drmRandom()
133 drm_public double drmRandomDouble(void *state) in drmRandomDouble() argument
135 RandomState *s = (RandomState *)state; in drmRandomDouble()
137 return (double)drmRandom(state)/(double)s->m; in drmRandomDouble()