imate
C++/CUDA Reference
split_mix_64.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 
16 #include "./split_mix_64.h"
17 #include "./highres_time_stamp.h" // get_highres_time_stamp
18 #include <cassert> // assert
19 
20 
21 // ===========
22 // Constructor
23 // ===========
24 
27 
28 SplitMix64::SplitMix64(const int64_t seed_)
29 {
30  // Seed the random generating algorithm with a high resolution time counter
31  uint64_t seed;
32 
33  if (seed_ >= 0)
34  {
35  seed = static_cast<uint64_t>(seed_);
36  }
37  else
38  {
39  // Negative integer is a flag to indicate using time to generate a seed
40  seed = get_highres_time_stamp();
41  }
42 
43  // Seeding as follow only fills the first 32 bits of the 64-bit integer.
44  // Repeat the first 32 bits on the second 32-bits to create a better 64-bit
45  // random number
46  this->state = (seed << 32) | seed;
47 }
48 
49 
50 // ====
51 // next
52 // ====
53 
56 
57 uint64_t SplitMix64::next()
58 {
59  uint64_t z = (state += 0x9e3779b97f4a7c15);
60  z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
61  z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
62 
63  return z ^ (z >> 31);
64 }
uint64_t state
Definition: split_mix_64.h:49
uint64_t next()
Generates the next presudo-random number in the sequence.
SplitMix64(const int64_t seed_)
Constructor. Initializes the state with current time.
uint64_t get_highres_time_stamp(void)