122944501Smrg/* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
222944501Smrg * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
322944501Smrg *
422944501Smrg * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
522944501Smrg * All Rights Reserved.
622944501Smrg *
722944501Smrg * Permission is hereby granted, free of charge, to any person obtaining a
822944501Smrg * copy of this software and associated documentation files (the "Software"),
922944501Smrg * to deal in the Software without restriction, including without limitation
1022944501Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1122944501Smrg * and/or sell copies of the Software, and to permit persons to whom the
1222944501Smrg * Software is furnished to do so, subject to the following conditions:
1322944501Smrg *
1422944501Smrg * The above copyright notice and this permission notice (including the next
1522944501Smrg * paragraph) shall be included in all copies or substantial portions of the
1622944501Smrg * Software.
1722944501Smrg *
1822944501Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1922944501Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2022944501Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2122944501Smrg * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2222944501Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2322944501Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2422944501Smrg * DEALINGS IN THE SOFTWARE.
2522944501Smrg *
2622944501Smrg * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
2722944501Smrg *
2822944501Smrg * DESCRIPTION
2922944501Smrg *
3022944501Smrg * This file contains a simple, straightforward implementation of the Park
3122944501Smrg * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
3222944501Smrg * multiplicative linear congruential generator (MLCG) with a period of
3322944501Smrg * 2^31-1.
3422944501Smrg *
3522944501Smrg * This implementation is intended to provide a reliable, portable PRNG
3622944501Smrg * that is suitable for testing a hash table implementation and for
3722944501Smrg * implementing skip lists.
3822944501Smrg *
3922944501Smrg * FUTURE ENHANCEMENTS
4022944501Smrg *
4122944501Smrg * If initial seeds are not selected randomly, two instances of the PRNG
4222944501Smrg * can be correlated.  [Knuth81, pp. 32-33] describes a shuffling technique
4322944501Smrg * that can eliminate this problem.
4422944501Smrg *
4522944501Smrg * If PRNGs are used for simulation, the period of the current
4622944501Smrg * implementation may be too short.  [LE88] discusses methods of combining
4722944501Smrg * MLCGs to produce much longer periods, and suggests some alternative
4822944501Smrg * values for A and M.  [LE90 and Sch92] also provide information on
4922944501Smrg * long-period PRNGs.
5022944501Smrg *
5122944501Smrg * REFERENCES
5222944501Smrg *
5322944501Smrg * [Knuth81] Donald E. Knuth. The Art of Computer Programming.  Volume 2:
5422944501Smrg * Seminumerical Algorithms.  Reading, Massachusetts: Addison-Wesley, 1981.
5522944501Smrg *
5622944501Smrg * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
5722944501Smrg * Generators".  CACM 31(6), June 1988, pp. 742-774.
5822944501Smrg *
5922944501Smrg * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
6022944501Smrg * October 1990, pp. 85-97.
6122944501Smrg *
6222944501Smrg * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
6322944501Smrg * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
6422944501Smrg *
6522944501Smrg * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
6622944501Smrg * CPUs".  Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
6722944501Smrg *
6822944501Smrg * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer.  In
6922944501Smrg * "Technical Correspondence: Remarks on Choosing and Implementing Random
7022944501Smrg * Number Generators". CACM 36(7), July 1993, pp. 105-110.
7122944501Smrg *
7222944501Smrg */
7322944501Smrg
7422944501Smrg#include <stdio.h>
7522944501Smrg#include <stdlib.h>
7622944501Smrg
777cdc0497Smrg#include "libdrm_macros.h"
78e6188e58Smrg#include "xf86drm.h"
79e6188e58Smrg#include "xf86drmRandom.h"
8022944501Smrg
8122944501Smrg#define RANDOM_MAGIC 0xfeedbeef
8222944501Smrg
837cdc0497Smrgdrm_public void *drmRandomCreate(unsigned long seed)
8422944501Smrg{
8522944501Smrg    RandomState  *state;
8622944501Smrg
87e6188e58Smrg    state           = drmMalloc(sizeof(*state));
8822944501Smrg    if (!state) return NULL;
8922944501Smrg    state->magic    = RANDOM_MAGIC;
9022944501Smrg#if 0
9122944501Smrg				/* Park & Miller, October 1988 */
9222944501Smrg    state->a        = 16807;
9322944501Smrg    state->m        = 2147483647;
9422944501Smrg    state->check    = 1043618065; /* After 10000 iterations */
9522944501Smrg#else
9622944501Smrg				/* Park, Miller, and Stockmeyer, July 1993 */
9722944501Smrg    state->a        = 48271;
9822944501Smrg    state->m        = 2147483647;
9922944501Smrg    state->check    = 399268537; /* After 10000 iterations */
10022944501Smrg#endif
10122944501Smrg    state->q        = state->m / state->a;
10222944501Smrg    state->r        = state->m % state->a;
10322944501Smrg
10422944501Smrg    state->seed     = seed;
10522944501Smrg				/* Check for illegal boundary conditions,
10622944501Smrg                                   and choose closest legal value. */
10722944501Smrg    if (state->seed <= 0)        state->seed = 1;
10822944501Smrg    if (state->seed >= state->m) state->seed = state->m - 1;
10922944501Smrg
11022944501Smrg    return state;
11122944501Smrg}
11222944501Smrg
1137cdc0497Smrgdrm_public int drmRandomDestroy(void *state)
11422944501Smrg{
115e6188e58Smrg    drmFree(state);
11622944501Smrg    return 0;
11722944501Smrg}
11822944501Smrg
1197cdc0497Smrgdrm_public unsigned long drmRandom(void *state)
12022944501Smrg{
12122944501Smrg    RandomState   *s = (RandomState *)state;
122e6188e58Smrg    unsigned long hi;
123e6188e58Smrg    unsigned long lo;
12422944501Smrg
12522944501Smrg    hi      = s->seed / s->q;
12622944501Smrg    lo      = s->seed % s->q;
12722944501Smrg    s->seed = s->a * lo - s->r * hi;
128e6188e58Smrg    if ((s->a * lo) <= (s->r * hi)) s->seed += s->m;
12922944501Smrg
13022944501Smrg    return s->seed;
13122944501Smrg}
13222944501Smrg
1337cdc0497Smrgdrm_public double drmRandomDouble(void *state)
13422944501Smrg{
13522944501Smrg    RandomState *s = (RandomState *)state;
13622944501Smrg
13722944501Smrg    return (double)drmRandom(state)/(double)s->m;
13822944501Smrg}
139