xf86drmRandom.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
  2. * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
  3. *
  4. * SPDX-FileCopyrightText: 1999 Precision Insight, Inc., Cedar Park, Texas.
  5. * SPDX-License-Identifier: MIT
  6. * SPDX-FileContributor: Rickard E. (Rik) Faith <faith@valinux.com>
  7. *
  8. * DESCRIPTION
  9. *
  10. * This file contains a simple, straightforward implementation of the Park
  11. * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
  12. * multiplicative linear congruential generator (MLCG) with a period of
  13. * 2^31-1.
  14. *
  15. * This implementation is intended to provide a reliable, portable PRNG
  16. * that is suitable for testing a hash table implementation and for
  17. * implementing skip lists.
  18. *
  19. * FUTURE ENHANCEMENTS
  20. *
  21. * If initial seeds are not selected randomly, two instances of the PRNG
  22. * can be correlated. [Knuth81, pp. 32-33] describes a shuffling technique
  23. * that can eliminate this problem.
  24. *
  25. * If PRNGs are used for simulation, the period of the current
  26. * implementation may be too short. [LE88] discusses methods of combining
  27. * MLCGs to produce much longer periods, and suggests some alternative
  28. * values for A and M. [LE90 and Sch92] also provide information on
  29. * long-period PRNGs.
  30. *
  31. * REFERENCES
  32. *
  33. * [Knuth81] Donald E. Knuth. The Art of Computer Programming. Volume 2:
  34. * Seminumerical Algorithms. Reading, Massachusetts: Addison-Wesley, 1981.
  35. *
  36. * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
  37. * Generators". CACM 31(6), June 1988, pp. 742-774.
  38. *
  39. * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
  40. * October 1990, pp. 85-97.
  41. *
  42. * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
  43. * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
  44. *
  45. * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
  46. * CPUs". Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
  47. *
  48. * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer. In
  49. * "Technical Correspondence: Remarks on Choosing and Implementing Random
  50. * Number Generators". CACM 36(7), July 1993, pp. 105-110.
  51. *
  52. */
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include "libdrm_macros.h"
  56. #include "xf86drm.h"
  57. #include "xf86drmRandom.h"
  58. #define RANDOM_MAGIC 0xfeedbeef
  59. drm_public void *drmRandomCreate(unsigned long seed)
  60. {
  61. RandomState *state;
  62. state = drmMalloc(sizeof(*state));
  63. if (!state) return NULL;
  64. state->magic = RANDOM_MAGIC;
  65. #if 0
  66. /* Park & Miller, October 1988 */
  67. state->a = 16807;
  68. state->m = 2147483647;
  69. state->check = 1043618065; /* After 10000 iterations */
  70. #else
  71. /* Park, Miller, and Stockmeyer, July 1993 */
  72. state->a = 48271;
  73. state->m = 2147483647;
  74. state->check = 399268537; /* After 10000 iterations */
  75. #endif
  76. state->q = state->m / state->a;
  77. state->r = state->m % state->a;
  78. state->seed = seed;
  79. /* Check for illegal boundary conditions,
  80. and choose closest legal value. */
  81. if (state->seed <= 0) state->seed = 1;
  82. if (state->seed >= state->m) state->seed = state->m - 1;
  83. return state;
  84. }
  85. drm_public int drmRandomDestroy(void *state)
  86. {
  87. drmFree(state);
  88. return 0;
  89. }
  90. drm_public unsigned long drmRandom(void *state)
  91. {
  92. RandomState *s = (RandomState *)state;
  93. unsigned long hi;
  94. unsigned long lo;
  95. hi = s->seed / s->q;
  96. lo = s->seed % s->q;
  97. s->seed = s->a * lo - s->r * hi;
  98. if ((s->a * lo) <= (s->r * hi)) s->seed += s->m;
  99. return s->seed;
  100. }
  101. drm_public double drmRandomDouble(void *state)
  102. {
  103. RandomState *s = (RandomState *)state;
  104. return (double)drmRandom(state)/(double)s->m;
  105. }