imate
C++/CUDA Reference
Loading...
Searching...
No Matches
random_number_generator.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3 * SPDX-License-Identifier: BSD-3-Clause
4 * SPDX-FileType: SOURCE
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the license found in the LICENSE.txt file in the root
8 * directory of this source tree.
9 */
10
11
12// =======
13// Headers
14// =======
15
17#include <cassert> // assert
18#include <cstdlib> // NULL
19#include "./xoshiro_256_star_star.h" // Xoshiro256StarStar
20
21
22// =============
23// Constructor 1
24// =============
25
28
30 num_threads(1)
31{
32 int64_t seed = -1; // Negative indicates using processor time as seed
33 this->initialize(seed);
34}
35
36
37// =============
38// Constructor 2
39// =============
40
47
49 num_threads(num_threads_)
50{
51 int64_t seed = -1; // Negative indicates using processor time as seed
52 this->initialize(seed);
53}
54
55
56// =============
57// Constructor 3
58// =============
59
69
71 const int num_threads_,
72 const int64_t seed):
73 num_threads(num_threads_)
74{
75 this->initialize(seed);
76}
77
78
79// ==========
80// Destructor
81// ==========
82
85
87{
88 if (this->xoshiro_256_star_star != NULL)
89 {
90 for (int thread_id=0; thread_id < this->num_threads; ++thread_id)
91 {
92 delete this->xoshiro_256_star_star[thread_id];
93 this->xoshiro_256_star_star[thread_id] = NULL;
94 }
95
96 delete[] this->xoshiro_256_star_star;
97 this->xoshiro_256_star_star = NULL;
98 }
99}
100
101
102// ==========
103// initialize
104// ==========
105
119
121{
122 assert(this->num_threads > 0);
123
125
126 for (int thread_id=0; thread_id < this->num_threads; ++thread_id)
127 {
128 this->xoshiro_256_star_star[thread_id] = new Xoshiro256StarStar(seed);
129
130 // Repeate jump j times to have different initial state for each thread
131 // This is the main purpose of this class.
132 for (int j=0; j < thread_id+1; ++j)
133 {
134 this->xoshiro_256_star_star[thread_id]->jump();
135 }
136 }
137}
138
139
140// ====
141// next
142// ====
143
149
150uint64_t RandomNumberGenerator::next(const int thread_id)
151{
152 return this->xoshiro_256_star_star[thread_id]->next();
153}
void initialize(int64_t seed)
Initializes an array of xoshiro_256_star_star objects.
Xoshiro256StarStar ** xoshiro_256_star_star
RandomNumberGenerator()
Initializes with one parallel thread and default seed.
uint64_t next(const int thread_id)
Generates the next random number in the sequence, depending on the thread id.
~RandomNumberGenerator()
Deallocates the array of xoshiro_256_star_star.
Pseudo-random integer generator. This class generates 64-bit integer using Xoshiro256** algorithm.
uint64_t next()
Generates the next presudo-random number.
void jump()
Jump function for the generator. It is equivalent to 2^128 calls to next(); it can be used to generat...