1*4882a593Smuzhiyun /* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
2*4882a593Smuzhiyun * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5*4882a593Smuzhiyun * All Rights Reserved.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
8*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
9*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
10*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
12*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
15*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
16*4882a593Smuzhiyun * Software.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21*4882a593Smuzhiyun * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * DESCRIPTION
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * This file contains a simple, straightforward implementation of the Park
31*4882a593Smuzhiyun * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
32*4882a593Smuzhiyun * multiplicative linear congruential generator (MLCG) with a period of
33*4882a593Smuzhiyun * 2^31-1.
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * This implementation is intended to provide a reliable, portable PRNG
36*4882a593Smuzhiyun * that is suitable for testing a hash table implementation and for
37*4882a593Smuzhiyun * implementing skip lists.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * FUTURE ENHANCEMENTS
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun * If initial seeds are not selected randomly, two instances of the PRNG
42*4882a593Smuzhiyun * can be correlated. [Knuth81, pp. 32-33] describes a shuffling technique
43*4882a593Smuzhiyun * that can eliminate this problem.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * If PRNGs are used for simulation, the period of the current
46*4882a593Smuzhiyun * implementation may be too short. [LE88] discusses methods of combining
47*4882a593Smuzhiyun * MLCGs to produce much longer periods, and suggests some alternative
48*4882a593Smuzhiyun * values for A and M. [LE90 and Sch92] also provide information on
49*4882a593Smuzhiyun * long-period PRNGs.
50*4882a593Smuzhiyun *
51*4882a593Smuzhiyun * REFERENCES
52*4882a593Smuzhiyun *
53*4882a593Smuzhiyun * [Knuth81] Donald E. Knuth. The Art of Computer Programming. Volume 2:
54*4882a593Smuzhiyun * Seminumerical Algorithms. Reading, Massachusetts: Addison-Wesley, 1981.
55*4882a593Smuzhiyun *
56*4882a593Smuzhiyun * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
57*4882a593Smuzhiyun * Generators". CACM 31(6), June 1988, pp. 742-774.
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
60*4882a593Smuzhiyun * October 1990, pp. 85-97.
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
63*4882a593Smuzhiyun * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
66*4882a593Smuzhiyun * CPUs". Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
67*4882a593Smuzhiyun *
68*4882a593Smuzhiyun * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer. In
69*4882a593Smuzhiyun * "Technical Correspondence: Remarks on Choosing and Implementing Random
70*4882a593Smuzhiyun * Number Generators". CACM 36(7), July 1993, pp. 105-110.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #include <stdio.h>
75*4882a593Smuzhiyun #include <stdlib.h>
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #include "libdrm_macros.h"
78*4882a593Smuzhiyun #include "xf86drm.h"
79*4882a593Smuzhiyun #include "xf86drmRandom.h"
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #define RANDOM_MAGIC 0xfeedbeef
82*4882a593Smuzhiyun
drmRandomCreate(unsigned long seed)83*4882a593Smuzhiyun drm_public void *drmRandomCreate(unsigned long seed)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun RandomState *state;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun state = drmMalloc(sizeof(*state));
88*4882a593Smuzhiyun if (!state) return NULL;
89*4882a593Smuzhiyun state->magic = RANDOM_MAGIC;
90*4882a593Smuzhiyun #if 0
91*4882a593Smuzhiyun /* Park & Miller, October 1988 */
92*4882a593Smuzhiyun state->a = 16807;
93*4882a593Smuzhiyun state->m = 2147483647;
94*4882a593Smuzhiyun state->check = 1043618065; /* After 10000 iterations */
95*4882a593Smuzhiyun #else
96*4882a593Smuzhiyun /* Park, Miller, and Stockmeyer, July 1993 */
97*4882a593Smuzhiyun state->a = 48271;
98*4882a593Smuzhiyun state->m = 2147483647;
99*4882a593Smuzhiyun state->check = 399268537; /* After 10000 iterations */
100*4882a593Smuzhiyun #endif
101*4882a593Smuzhiyun state->q = state->m / state->a;
102*4882a593Smuzhiyun state->r = state->m % state->a;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun state->seed = seed;
105*4882a593Smuzhiyun /* Check for illegal boundary conditions,
106*4882a593Smuzhiyun and choose closest legal value. */
107*4882a593Smuzhiyun if (state->seed <= 0) state->seed = 1;
108*4882a593Smuzhiyun if (state->seed >= state->m) state->seed = state->m - 1;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun return state;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
drmRandomDestroy(void * state)113*4882a593Smuzhiyun drm_public int drmRandomDestroy(void *state)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun drmFree(state);
116*4882a593Smuzhiyun return 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
drmRandom(void * state)119*4882a593Smuzhiyun drm_public unsigned long drmRandom(void *state)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun RandomState *s = (RandomState *)state;
122*4882a593Smuzhiyun unsigned long hi;
123*4882a593Smuzhiyun unsigned long lo;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun hi = s->seed / s->q;
126*4882a593Smuzhiyun lo = s->seed % s->q;
127*4882a593Smuzhiyun s->seed = s->a * lo - s->r * hi;
128*4882a593Smuzhiyun if ((s->a * lo) <= (s->r * hi)) s->seed += s->m;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun return s->seed;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
drmRandomDouble(void * state)133*4882a593Smuzhiyun drm_public double drmRandomDouble(void *state)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun RandomState *s = (RandomState *)state;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun return (double)drmRandom(state)/(double)s->m;
138*4882a593Smuzhiyun }
139