pcg_basic.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * PCG Random Number Generation for C.
  3. *
  4. * Copyright 2014 Melissa O'Neill <oneill@pcg-random.org>
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * For additional information about the PCG random number generation scheme,
  19. * including its license and other licensing options, visit
  20. *
  21. * http://www.pcg-random.org
  22. */
  23. /*
  24. * This code is derived from the full C implementation, which is in turn
  25. * derived from the canonical C++ PCG implementation. The C++ version
  26. * has many additional features and is preferable if you can use C++ in
  27. * your project.
  28. */
  29. #ifndef PCG_BASIC_H_INCLUDED
  30. #define PCG_BASIC_H_INCLUDED 1
  31. #include <inttypes.h>
  32. #if __cplusplus
  33. extern "C" {
  34. #endif
  35. struct pcg_state_setseq_64 { // Internals are *Private*.
  36. uint64_t state; // RNG state. All values are possible.
  37. uint64_t inc; // Controls which RNG sequence (stream) is
  38. // selected. Must *always* be odd.
  39. };
  40. typedef struct pcg_state_setseq_64 pcg32_random_t;
  41. // If you *must* statically initialize it, here's one.
  42. #define PCG32_INITIALIZER { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }
  43. // pcg32_srandom(initial_state, initseq)
  44. // pcg32_srandom_r(rng, initial_state, initseq):
  45. // Seed the rng. Specified in two parts, state initializer and a
  46. // sequence selection constant (a.k.a. stream id)
  47. void pcg32_srandom(uint64_t initial_state, uint64_t initseq);
  48. void pcg32_srandom_r(pcg32_random_t* rng, uint64_t initial_state,
  49. uint64_t initseq);
  50. // pcg32_random()
  51. // pcg32_random_r(rng)
  52. // Generate a uniformly distributed 32-bit random number
  53. uint32_t pcg32_random(void);
  54. uint32_t pcg32_random_r(pcg32_random_t* rng);
  55. // pcg32_boundedrand(bound):
  56. // pcg32_boundedrand_r(rng, bound):
  57. // Generate a uniformly distributed number, r, where 0 <= r < bound
  58. uint32_t pcg32_boundedrand(uint32_t bound);
  59. uint32_t pcg32_boundedrand_r(pcg32_random_t* rng, uint32_t bound);
  60. #if __cplusplus
  61. }
  62. #endif
  63. #endif // PCG_BASIC_H_INCLUDED