-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
44 lines (33 loc) · 976 Bytes
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <algorithm>
#include <random>
#include <cmath>
#include <ctime>
#include "utils.hpp"
using namespace std;
// mt19937 mte(1730); // Fixed seed for determinism
mt19937 mte(time(NULL)); // Initialize seed with time
Vec uniform_on_sphere(size_t dim)
{
/*
Generates random normalized vectors. This code uses normal
distributions, then normalize them to ensure uniformity
(compared to other methods like normalizing an uniform
distribution)
*/
normal_distribution<double> ndist;
Vec vec(3);
generate(begin(vec), end(vec), [&](){ return ndist(mte); });
return vec / sqrt(vec * vec).sum();
}
std::valarray<size_t> random_site(size_t N, size_t dim)
{
uniform_int_distribution<size_t> udist{0, N-1};
std::valarray<size_t> v(3);
generate(begin(v), end(v), [&](){return udist(mte); });
return v;
}
double uniform_unit()
{
static uniform_real_distribution<double> udist;
return udist(mte);
}