PetaVision  Alpha
RandomSeed.cpp
1 /*
2  * RandomSeed.cpp
3  *
4  * Created on: Jul 26, 2016
5  * Author: pschultz
6  */
7 
8 #include "columns/RandomSeed.hpp"
9 #include "utils/PVLog.hpp"
10 
11 namespace PV {
12 
13 RandomSeed *RandomSeed::instance() {
14  static RandomSeed *singleton = new RandomSeed();
15  return singleton;
16 }
17 
18 RandomSeed::RandomSeed() {
19  if (sizeof(unsigned int) < (size_t)4) {
20  Fatal() << "Unsigned int must have a size of at least 4 bytes.\n";
21  }
22 }
23 
24 void RandomSeed::initialize(unsigned int initialSeed) {
25  if (initialSeed < RandomSeed::minSeed) {
26  Fatal() << "random seed " << initialSeed << ""
27  " is too small. Use a seed of at least "
28  << minSeed << ".\n";
29  }
30  mInitialized = true;
31  mInitialSeed = initialSeed;
32  mNextSeed = initialSeed;
33 }
34 
35 unsigned int RandomSeed::allocate(unsigned int numRequested) {
36  if (!mInitialized) {
37  Fatal() << "RandomSeed has not been initialized.\n";
38  }
39  unsigned int allocation = mNextSeed;
40  mNextSeed += numRequested;
41  if (mNextSeed < minSeed) {
42  mNextSeed += minSeed;
43  }
44  return allocation;
45 }
46 
47 unsigned int constexpr RandomSeed::minSeed;
48 } /* namespace PV */