xopto.cl.clrng module¶
- class Random(primes=None, forcepy=False)[source]¶
Bases:
object
Create an instance of a random number generator.
Class depends on external rng64.dll (rng64.so) and rng32.dll (rng32.so) that can be generated/built by calling
xopto.rebuild()
. The built shared library will be placed intoxopto.USER_BIN_PATH
folder. If an external library is not found, a slower python implementation of the required functions is used instead.- Parameters
primes (np.ndarray) – If provided, the primes must be organized into a numpy array of shape (nprimes, 2). Type of the array should be uint32 or uint64. See the one of the safeprimes_base32_<>.npz files for more details.
forcepy (bool) – If True, use python implementation of the random number helper functions even if a shared library is available.
Note
Use the seeds method to generate seed pairs X and A for a a GPU-based random number generator. Each GPU thread should take one pair of values, e.g. X[i], A[i], to use with the random number generator. Note that the value of seed x is changed on each call to the random number generator and should be saved before the GPU thread returns, e.g. to the global memory. The value of seed a is not changed during the calls to the random number generator.
Examples of different uniform random number generators can be found in xopto/cl/kernels/ml.h file.
- NPRIMES = 500000¶
Default number of primes to generate.
- PRIMES_FILE = 'safeprimes_base32_500k.npz'¶
Default npz file with primes.
- cc(n: int, x: Optional[int] = None, a: Optional[int] = None) → Tuple[numpy.ndarray, int, int][source]¶
Generate n random numbers from [0.0, 1.0] using seeds x and a. If provided, the two seeds should be generated by the
seeds()
method function.- Parameters
n (int) – Generates n random number.
x (int) – 64-bit unsigned integer seed updated and returned on completion.
a (int) – 32-bit unsigned integer seed updated and returned on completion.
- Returns
data (np.float32) – A vector of n random numbers.
xnew (int) – Updated value of the input seed x.
a (int) – The input seed a.
- co(n, x: Optional[int] = None, a: Optional[int] = None) → Tuple[numpy.ndarray, int, int][source]¶
Generate n random numbers from [0.0, 1.0) using seeds x and a. If provided, the two seeds should be generated by the
seeds()
method.- Parameters
n (int) – Generates n random number.
x (int) – 64-bit unsigned integer seed updated and returned on completion.
a (int) – 32-bit unsigned integer seed updated and returned on completion.
- Returns
data (np.float32) – A vector of n random numbers.
xnew (int) – Updated value of the input seed x.
a (int) – The input seed a.
- initializer()[source]¶
Creates an initializer (32-bit unsigned integer) required to compute seeds (see the seeds method) of the random number generator.
- property maxseeds: int¶
Maximum number of random generator seeds that can be produced by the available primes.
- oc(n, x: Optional[int] = None, a: Optional[int] = None) → Tuple[numpy.ndarray, int, int][source]¶
Generate n random numbers from (0.0, 1.0] using seeds x and a. If provided, the two seeds shoud be generated by the
seeds()
method.- Parameters
n (int) – Generates n random number.
x (int) – 64-bit unsigned integer seed updated and returned on completion.
a (int) – 32-bit unsigned integer seed updated and returned on completion.
- Returns
data (np.float32) – A vector of n random numbers.
xnew (int) – Updated value of the input seed x.
a (int) – The input seed a.
- oo(n, x: Optional[int] = None, a: Optional[int] = None) → Tuple[numpy.ndarray, int, int][source]¶
Generate n random numbers from (0.0, 1.0) using seeds x and a. If provided, the two seeds should be generated by the
seeds()
method.- Parameters
n (int) – Generates n random number.
x (int) – 64-bit unsigned integer seed updated and returned on completion.
a (int) – 32-bit unsigned integer seed updated and returned on completion.
- Returns
data (np.float32) – A vector of n random numbers.
xnew (int) – Updated value of the input seed x.
a (int) – The input seed a.
- seeds(n: int = - 1, xinit: Optional[numpy.uint64] = None) → Tuple[numpy.ndarray, numpy.ndarray][source]¶
Generates the requested number of seeds. Each generator requires two initializer/seeds (x, a), one 64-bit unsigned integer (x) and one 32-bit unsigned integer (a). The seed x is updated on each call to the random number generator, while seed a remains unchanged.
- Parameters
n (int) – The requested number of seeds - must not exceed the value of property
maxseeds
. Use -1 to generate all available seeds.xinit (int) – A 64-bit unsigned integer random number as initializer. If none is provided the builtin numpy random generator is used to create one.
- Returns
x (np.uint64 vector) – A vector of n 64-bit unsigned integers representing x seeds.
a (np.uint32 vector) – A vector of n 32-bit unsigned integers representing a seeds.
Note
Each random number generator should be initialized by a pair odf seeds x[i], a[i].
- generate_primes(n: int, out: Optional[numpy.ndarray] = None, verbose: bool = False) → numpy.ndarray[source]¶
Generates n safe primes used by the random number generator.
- Parameters
n (int) – Number of safe primes to generate.
out (np.ndarray of type np.uint64) – Optional output array for safe primes that must be of type np.uint64 and shape (3, n).
verbose (bool) – Verbose mode.
- Returns
out – Output array with the safe random numbers.
- Return type
np.ndarray of type np.uint64
Note
Generating a typical batch of 150,000 primes takes about 5 min. This function requires gmpy2 module.