Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

79 lines (69 sloc) 2.153 kb
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("");
}
}
Jump to Line
Something went wrong with that request. Please try again.