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.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "modules.h"
|
||||
|
||||
#include "io.wren.inc"
|
||||
#include "random.wren.inc"
|
||||
#include "scheduler.wren.inc"
|
||||
#include "timer.wren.inc"
|
||||
|
||||
@@ -15,6 +16,12 @@ extern void fileClose(WrenVM* vm);
|
||||
extern void fileDescriptor(WrenVM* vm);
|
||||
extern void fileReadBytes(WrenVM* vm);
|
||||
extern void fileSize(WrenVM* vm);
|
||||
extern void randomAllocate(WrenVM* vm);
|
||||
extern void randomSeed0(WrenVM* vm);
|
||||
extern void randomSeed1(WrenVM* vm);
|
||||
extern void randomSeed16(WrenVM* vm);
|
||||
extern void randomFloat(WrenVM* vm);
|
||||
extern void randomInt0(WrenVM* vm);
|
||||
extern void stdinReadStart(WrenVM* vm);
|
||||
extern void stdinReadStop(WrenVM* vm);
|
||||
extern void schedulerCaptureMethods(WrenVM* vm);
|
||||
@@ -98,6 +105,16 @@ static ModuleRegistry modules[] =
|
||||
STATIC_METHOD("readStop_()", stdinReadStop)
|
||||
END_CLASS
|
||||
END_MODULE
|
||||
MODULE(random)
|
||||
CLASS(Random)
|
||||
STATIC_METHOD("<allocate>", randomAllocate)
|
||||
METHOD("seed_()", randomSeed0)
|
||||
METHOD("seed_(_)", randomSeed1)
|
||||
METHOD("seed_(_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_)", randomSeed16)
|
||||
METHOD("float()", randomFloat)
|
||||
METHOD("int()", randomInt0)
|
||||
END_CLASS
|
||||
END_MODULE
|
||||
MODULE(scheduler)
|
||||
CLASS(Scheduler)
|
||||
STATIC_METHOD("captureMethods_()", schedulerCaptureMethods)
|
||||
|
||||
Reference in New Issue
Block a user