-
Notifications
You must be signed in to change notification settings - Fork 3
/
mersenne.winxed
78 lines (69 loc) · 2.1 KB
/
mersenne.winxed
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
namespace Rosella.Random
{
extern function default_uniform_random;
extern function default_normal_random;
}
// Mersenne Twister example.
function main[main]()
{
var rosella = load_packfile("rosella/core.pbc");
var(Rosella.initialize_rosella)("random");
var rnd = Rosella.Random.default_uniform_random();
print("\n20 random uniformly-distributed integers: ");
for(int i = 0; i < 20; i++)
{
print(rnd.get_int());
print(" ");
}
say("");
print("\n20 random uniformly-distributed floats: ");
for(int i = 0; i < 20; i++)
{
print(rnd.get_float());
print(" ");
}
say("");
print("\n20 random uniformly-distributed ints in [0, 10): ");
for(int i = 0; i < 20; i++)
{
print(rnd.get_range(0, 10));
print(" ");
}
say("");
print("\nHistogram of 500 uniformly-distributed floats: ");
int buckets_a[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for (int i = 0; i < 500; i++) {
int idx = rnd.get_range(0, 21);
if (idx >= 0 && idx < elements(buckets_a))
buckets_a[idx] = buckets_a[idx] + 1;
}
for (int i = 0; i < elements(buckets_a); i++) {
print(i, ": ");
for (int j = 0; j < int(buckets_a[i]); j++)
print("#");
say("");
}
rnd = Rosella.Random.default_normal_random(0.00, 0.25);
print("\n20 random normal-distributed floats: ");
for (int i = 0; i < 20; i++) {
print(rnd.get_float());
print(" ");
}
say("");
rnd = Rosella.Random.default_normal_random(10.00, 3.00);
print("\nHistogram of 500 normal-distributed floats: ");
int buckets[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for (int i = 0; i < 500; i++) {
float value = rnd.get_float() + 0.5;
${ floor value, value };
int idx = int(value);
if (idx >= 0 && idx < elements(buckets))
buckets[idx] = buckets[idx] + 1;
}
for (int i = 0; i < elements(buckets); i++) {
print(i, ": ");
for (int j = 0; j < int(buckets[i]); j++)
print("#");
say("");
}
}