feat: add WELL512-based Random module with seed, float, and int methods
Implement a new `random` module for the Wren language runtime, providing a `Random` class backed by the WELL512a PRNG algorithm. The module supports three seeding strategies (time-based, single numeric seed, and 16-element sequence), generates uniform floats in [0,1) and integers in the full uint32 range, and includes convenience methods for bounded ranges. The implementation adds the C foreign functions `randomAllocate`, `randomSeed0`, `randomSeed1`, `randomSeed16`, `randomFloat`, and `randomInt0`, registers them in the module registry, and includes the Wren wrapper class with constructors accepting no arguments, a number, or a sequence. The commit also adds 12 test scripts covering float/int generation, seeding reproducibility, and error handling for invalid seed types.
2015-10-17 20:03:15 +02:00
|
|
|
foreign class Random {
|
|
|
|
|
construct new() {
|
|
|
|
|
seed_()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
construct new(seed) {
|
|
|
|
|
if (seed is Num) {
|
|
|
|
|
seed_(seed)
|
|
|
|
|
} else if (seed is Sequence) {
|
|
|
|
|
if (seed.isEmpty) Fiber.abort("Sequence cannot be empty.")
|
|
|
|
|
|
|
|
|
|
// TODO: Empty sequence.
|
|
|
|
|
var seeds = []
|
|
|
|
|
for (element in seed) {
|
|
|
|
|
if (!(element is Num)) Fiber.abort("Sequence elements must all be numbers.")
|
|
|
|
|
|
|
|
|
|
seeds.add(element)
|
|
|
|
|
if (seeds.count == 16) break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Cycle the values to fill in any missing slots.
|
|
|
|
|
var i = 0
|
|
|
|
|
while (seeds.count < 16) {
|
|
|
|
|
seeds.add(seeds[i])
|
|
|
|
|
i = i + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
seed_(
|
|
|
|
|
seeds[0], seeds[1], seeds[2], seeds[3],
|
|
|
|
|
seeds[4], seeds[5], seeds[6], seeds[7],
|
|
|
|
|
seeds[8], seeds[9], seeds[10], seeds[11],
|
|
|
|
|
seeds[12], seeds[13], seeds[14], seeds[15])
|
|
|
|
|
} else {
|
|
|
|
|
Fiber.abort("Seed must be a number or a sequence of numbers.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreign seed_()
|
|
|
|
|
foreign seed_(seed)
|
|
|
|
|
foreign seed_(n1, n2, n3, n4, n5, n6, n7, n8, n9, n10, n11, n12, n13, n14, n15, n16)
|
|
|
|
|
|
|
|
|
|
foreign float()
|
2015-11-09 16:37:06 +01:00
|
|
|
float(end) { float() * end }
|
|
|
|
|
float(start, end) { float() * (end - start) + start }
|
feat: add WELL512-based Random module with seed, float, and int methods
Implement a new `random` module for the Wren language runtime, providing a `Random` class backed by the WELL512a PRNG algorithm. The module supports three seeding strategies (time-based, single numeric seed, and 16-element sequence), generates uniform floats in [0,1) and integers in the full uint32 range, and includes convenience methods for bounded ranges. The implementation adds the C foreign functions `randomAllocate`, `randomSeed0`, `randomSeed1`, `randomSeed16`, `randomFloat`, and `randomInt0`, registers them in the module registry, and includes the Wren wrapper class with constructors accepting no arguments, a number, or a sequence. The commit also adds 12 test scripts covering float/int generation, seeding reproducibility, and error handling for invalid seed types.
2015-10-17 20:03:15 +02:00
|
|
|
|
|
|
|
|
foreign int()
|
2015-11-09 16:37:06 +01:00
|
|
|
int(end) { (float() * end).floor }
|
|
|
|
|
int(start, end) { (float() * (end - start)).floor + start }
|
feat: add WELL512-based Random module with seed, float, and int methods
Implement a new `random` module for the Wren language runtime, providing a `Random` class backed by the WELL512a PRNG algorithm. The module supports three seeding strategies (time-based, single numeric seed, and 16-element sequence), generates uniform floats in [0,1) and integers in the full uint32 range, and includes convenience methods for bounded ranges. The implementation adds the C foreign functions `randomAllocate`, `randomSeed0`, `randomSeed1`, `randomSeed16`, `randomFloat`, and `randomInt0`, registers them in the module registry, and includes the Wren wrapper class with constructors accepting no arguments, a number, or a sequence. The commit also adds 12 test scripts covering float/int generation, seeding reproducibility, and error handling for invalid seed types.
2015-10-17 20:03:15 +02:00
|
|
|
}
|