Skip to content
Newer
Older
100644 79 lines (69 sloc) 2.05 KB
87e8ec4 @Whiteknight [Random] A few small tweaks and fixes for MersenneTwister, hopefully …
authored
1 $include "Rosella/Core.winxed";
2 $include "Rosella/Random.winxed";
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
3
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
4 // Mersenne Twister example.
5
87e8ec4 @Whiteknight [Random] A few small tweaks and fixes for MersenneTwister, hopefully …
authored
6 function mersenne_main()
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
7 {
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
8 var rnd = Rosella.Random.default_uniform_random();
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
9
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
10 print("\n20 random uniformly-distributed integers: ");
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
11 for(int i = 0; i < 20; i++)
12 {
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
13 print(rnd.get_int());
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
14 print(" ");
15 }
16 say("");
17
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
18 print("\n20 random uniformly-distributed floats: ");
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
19 for(int i = 0; i < 20; i++)
20 {
21 print(rnd.get_float());
22 print(" ");
23 }
24 say("");
25
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
26 print("\n20 random uniformly-distributed ints in [0, 10): ");
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
27 for(int i = 0; i < 20; i++)
28 {
29 print(rnd.get_range(0, 10));
30 print(" ");
31 }
32 say("");
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
33
87e8ec4 @Whiteknight [Random] A few small tweaks and fixes for MersenneTwister, hopefully …
authored
34 say("\nHistogram of 500 uniformly-distributed floats: ");
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
35 int buckets_a[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
36 for (int i = 0; i < 500; i++) {
37 int idx = rnd.get_range(0, 21);
38 if (idx >= 0 && idx < elements(buckets_a))
39 buckets_a[idx] = buckets_a[idx] + 1;
40 }
41 for (int i = 0; i < elements(buckets_a); i++) {
42 print(i, ": ");
43 for (int j = 0; j < int(buckets_a[i]); j++)
44 print("#");
45 say("");
46 }
47
9635fe2 @Whiteknight fix the interface for the normal number generator to be the same as t…
authored
48 rnd = Rosella.Random.default_normal_random(0.00, 0.25);
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
49 print("\n20 random normal-distributed floats: ");
50 for (int i = 0; i < 20; i++) {
9635fe2 @Whiteknight fix the interface for the normal number generator to be the same as t…
authored
51 print(rnd.get_float());
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
52 print(" ");
53 }
54 say("");
55
9635fe2 @Whiteknight fix the interface for the normal number generator to be the same as t…
authored
56 rnd = Rosella.Random.default_normal_random(10.00, 3.00);
87e8ec4 @Whiteknight [Random] A few small tweaks and fixes for MersenneTwister, hopefully …
authored
57 say("\nHistogram of 500 normal-distributed floats: ");
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
58 int buckets[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
59 for (int i = 0; i < 500; i++) {
9635fe2 @Whiteknight fix the interface for the normal number generator to be the same as t…
authored
60 float value = rnd.get_float() + 0.5;
5a540ef @Whiteknight Add in a Box-Muller transformation to turn the Marsenne twister into …
authored
61 ${ floor value, value };
62 int idx = int(value);
63 if (idx >= 0 && idx < elements(buckets))
64 buckets[idx] = buckets[idx] + 1;
65 }
66 for (int i = 0; i < elements(buckets); i++) {
67 print(i, ": ");
68 for (int j = 0; j < int(buckets[i]); j++)
69 print("#");
70 say("");
71 }
98ffa77 @nol888 Add Mersenne Twister implementation.
nol888 authored
72 }
87e8ec4 @Whiteknight [Random] A few small tweaks and fixes for MersenneTwister, hopefully …
authored
73
74 function main[main]()
75 {
76 using mersenne_main;
77 Rosella.Parrot.try_report(mersenne_main);
78 }
Something went wrong with that request. Please try again.