| 1 |
#ifndef RBIMPL_INTERN_RANDOM_H /*-*-C++-*-vi:se ft=cpp:*/ |
| 2 |
#define RBIMPL_INTERN_RANDOM_H |
| 3 |
/** |
| 4 |
* @file |
| 5 |
* @author Ruby developers <[email protected]> |
| 6 |
* @copyright This file is a part of the programming language Ruby. |
| 7 |
* Permission is hereby granted, to either redistribute and/or |
| 8 |
* modify this file, provided that the conditions mentioned in the |
| 9 |
* file COPYING are met. Consult the file for details. |
| 10 |
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are |
| 11 |
* implementation details. Don't take them as canon. They could |
| 12 |
* rapidly appear then vanish. The name (path) of this header file |
| 13 |
* is also an implementation detail. Do not expect it to persist |
| 14 |
* at the place it is now. Developers are free to move it anywhere |
| 15 |
* anytime at will. |
| 16 |
* @note To ruby-core: remember that this header can be possibly |
| 17 |
* recursively included from extension libraries written in C++. |
| 18 |
* Do not expect for instance `__VA_ARGS__` is always available. |
| 19 |
* We assume C99 for ruby itself but we don't assume languages of |
| 20 |
* extension libraries. They could be written in C++98. |
| 21 |
* @brief MT19937 backended pseudo random number generator. |
| 22 |
* @see Matsumoto, M., Nishimura, T., "Mersenne Twister: A 623- |
| 23 |
* dimensionally equidistributed uniform pseudorandom number |
| 24 |
* generator", ACM Trans. on Modeling and Computer Simulation, 8 |
| 25 |
* (1): pp 3-30, 1998. https://doi.org/10.1145/272991.272995 |
| 26 |
*/ |
| 27 |
#include "ruby/internal/dllexport.h" |
| 28 |
#include "ruby/internal/value.h" |
| 29 |
|
| 30 |
RBIMPL_SYMBOL_EXPORT_BEGIN() |
| 31 |
|
| 32 |
/* random.c */ |
| 33 |
|
| 34 |
/** |
| 35 |
* Generates a 32 bit random number. |
| 36 |
* |
| 37 |
* @return A random number. |
| 38 |
* @note Now that we have ractors, the RNG behind this function is |
| 39 |
* per-ractor. |
| 40 |
*/ |
| 41 |
unsigned int rb_genrand_int32(void); |
| 42 |
|
| 43 |
/** |
| 44 |
* Generates a `double` random number. |
| 45 |
* |
| 46 |
* @return A random number. |
| 47 |
* @note This function shares the RNG with rb_genrand_int32(). |
| 48 |
*/ |
| 49 |
double rb_genrand_real(void); |
| 50 |
|
| 51 |
/** |
| 52 |
* Resets the RNG behind rb_genrand_int32()/rb_genrand_real(). |
| 53 |
* |
| 54 |
* @post The (now per-ractor) default RNG's internal state is cleared. |
| 55 |
*/ |
| 56 |
void rb_reset_random_seed(void); |
| 57 |
|
| 58 |
/** |
| 59 |
* Generates a String of random bytes. |
| 60 |
* |
| 61 |
* @param[in,out] rnd An instance of ::rb_cRandom. |
| 62 |
* @param[in] n Requested number of bytes. |
| 63 |
* @return An instance of ::rb_cString, of binary, of `n` bytes length, |
| 64 |
* whose contents are random bits. |
| 65 |
* |
| 66 |
* @internal |
| 67 |
* |
| 68 |
* @shyouhei doesn't know if this is an Easter egg or an official feature, but |
| 69 |
* this function can take a wider range of objects, such as `Socket::Ifaddr`. |
| 70 |
* The arguments are just silently ignored and the default RNG is used instead, |
| 71 |
* if they are non-RNG. |
| 72 |
*/ |
| 73 |
VALUE rb_random_bytes(VALUE rnd, long n); |
| 74 |
|
| 75 |
/** |
| 76 |
* Identical to rb_genrand_int32(), except it generates using the passed RNG. |
| 77 |
* |
| 78 |
* @param[in,out] rnd An instance of ::rb_cRandom. |
| 79 |
* @return A random number. |
| 80 |
*/ |
| 81 |
unsigned int rb_random_int32(VALUE rnd); |
| 82 |
|
| 83 |
/** |
| 84 |
* Identical to rb_genrand_real(), except it generates using the passed RNG. |
| 85 |
* |
| 86 |
* @param[in,out] rnd An instance of ::rb_cRandom. |
| 87 |
* @return A random number. |
| 88 |
*/ |
| 89 |
double rb_random_real(VALUE rnd); |
| 90 |
|
| 91 |
/** |
| 92 |
* Identical to rb_genrand_ulong_limited(), except it generates using the |
| 93 |
* passed RNG. |
| 94 |
* |
| 95 |
* @param[in,out] rnd An instance of ::rb_cRandom. |
| 96 |
* @param[in] limit Max possible return value. |
| 97 |
* @return A random number, distributed in `[0, limit]` interval. |
| 98 |
* @note Note it can return `limit`. |
| 99 |
* @note Whether the return value distributes uniformly in the |
| 100 |
* interval or not depends on how the argument RNG behaves; at |
| 101 |
* least in case of MT19937 it does. |
| 102 |
*/ |
| 103 |
unsigned long rb_random_ulong_limited(VALUE rnd, unsigned long limit); |
| 104 |
|
| 105 |
/** |
| 106 |
* Generates a random number whose upper limit is `i`. |
| 107 |
* |
| 108 |
* @param[in] i Max possible return value. |
| 109 |
* @return A random number, uniformly distributed in `[0, limit]` interval. |
| 110 |
* @note Note it can return `i`. |
| 111 |
*/ |
| 112 |
unsigned long rb_genrand_ulong_limited(unsigned long i); |
| 113 |
|
| 114 |
RBIMPL_SYMBOL_EXPORT_END() |
| 115 |
|
| 116 |
#endif /* RBIMPL_INTERN_RANDOM_H */ |