PSCF v1.4.0
CpuVecRandom.cpp
1#include "CpuVecRandom.h"
2#include <util/random/Random.h>
3#include <util/containers/Array.h>
4#include <util/global.h>
5
6namespace Pscf {
7
8 using namespace Util;
9
10 /*
11 * Default constructor.
12 */
14 : randomPtr_(nullptr)
15 {}
16
17 /*
18 * Constructor - create association with a scalar RNG.
19 */
21 : randomPtr_(&random)
22 {}
23
24 /*
25 * Destructor.
26 */
29
30 /*
31 * Create an association after construction
32 */
34 { randomPtr_ = &random; }
35
36 /*
37 * Populate array on device with random doubles in (0, 1], uniform dist.
38 */
40 {
41 UTIL_CHECK(randomPtr_);
42 UTIL_CHECK(data.capacity() > 0);
43 const int n = data.capacity();
44 for (int i = 0; i < n; ++i) {
45 data[i] = randomPtr_->uniform();
46 }
47 }
48
49 /*
50 * Populate array with random doubles uniform dist in (min, max].
51 */
52 void CpuVecRandom::uniform(Array<double>& data, double min, double max)
53 {
54 UTIL_CHECK(randomPtr_);
55 UTIL_CHECK(data.capacity() > 0);
56 UTIL_CHECK(max > min);
57 const int n = data.capacity();
58 for (int i = 0; i < n; ++i) {
59 data[i] = randomPtr_->uniform(min, max);
60 }
61 }
62
63 /*
64 * Populate array with normal-distributed random doubles.
65 */
67 double stddev, double mean)
68 {
69 UTIL_CHECK(randomPtr_);
70 UTIL_CHECK(data.capacity() > 0);
71 const int n = data.capacity();
72 for (int i = 0; i < n; ++i) {
73 data[i] = mean + stddev * randomPtr_->gaussian();
74 }
75 }
76
77} // namespace Pscf
void normal(Array< double > &data, double stddev, double mean=0.0)
Generate normal-distributed random doubles.
void associate(Util::Random &random)
Create an association with a Util::Random scalar RNG.
CpuVecRandom()
Default constructor.
virtual ~CpuVecRandom()
Destructor.
void uniform(Array< double > &data)
Generate uniform random doubles in (0, 1].
Array container class template.
Definition Array.h:40
int capacity() const
Return allocated size.
Definition Array.h:144
Random number generator.
Definition Random.h:47
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
PSCF package top-level namespace.