Commit 680df8ea

680df8ea79bdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified

Description

runtime,analyze,codegen: Random class (per-instance PRNG)

Random.new(seed) caused a C compile error (no sp_Random type). Add a GC-managed sp_Random backed by a portable xorshift64 (rand_r is POSIX-only, absent on MinGW), plus rand(int)/rand/rand(float)/bytes instance methods and Random.rand/Random.bytes class-method forms over a lazily-seeded shared default. analyze grows a "random" type with the matching return types; codegen maps it to sp_Random *.

The sequence differs from CRuby's MT19937 (the issue acknowledges this), but each Random object keeps its own reproducible stream from its seed. bytes#length is subject to spinel's lack of ASCII-8BIT (String#length counts UTF-8 chars); the value itself is correct.

Fixes #898

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
680df8ea79bd
Parent
970a80a9fc3d

lib/sp_runtime.hmodifiedView file

@@ -3500,6 +3500,48 @@
35003500 alongside the hash type it closes over. */
35013501 static void sp_hashproc_cap_scan(void *p) { sp_gc_mark(p); }
35023502
3503+/* Random — per-instance PRNG. CRuby uses MT19937; spinel uses a
3504+ portable xorshift64 (rand_r is POSIX-only, absent on MinGW), so
3505+ the *sequence* differs from MRI but each Random object keeps its
3506+ own reproducible stream from its seed. Issue #898. */
3507+typedef struct { uint64_t state; } sp_Random;
3508+static uint64_t sp_random_next(sp_Random *r) {
3509+ uint64_t x = r->state ? r->state : 0x9E3779B97F4A7C15ULL;
3510+ x ^= x << 13; x ^= x >> 7; x ^= x << 17;
3511+ r->state = x;
3512+ return x;
3513+}
3514+static sp_Random *sp_Random_new(mrb_int seed) {
3515+ sp_Random *r = (sp_Random *)sp_gc_alloc(sizeof(sp_Random), NULL, NULL);
3516+ r->state = (uint64_t)seed ^ 0x9E3779B97F4A7C15ULL;
3517+ return r;
3518+}
3519+static mrb_int sp_Random_rand_int(sp_Random *r, mrb_int n) {
3520+ if (!r || n <= 0) return 0;
3521+ return (mrb_int)(sp_random_next(r) % (uint64_t)n);
3522+}
3523+static mrb_float sp_Random_rand_float(sp_Random *r) {
3524+ if (!r) return 0.0;
3525+ return (mrb_float)(sp_random_next(r) >> 11) / (mrb_float)(1ULL << 53);
3526+}
3527+/* Class-method forms (`Random.rand` / `Random.bytes`) share one
3528+ lazily-seeded default instance, mirroring CRuby's Random::DEFAULT. */
3529+static sp_Random sp_random_default = { 0 };
3530+static sp_Random *sp_random_default_get(void) {
3531+ if (sp_random_default.state == 0) sp_random_default.state = (uint64_t)time(NULL) ^ 0x9E3779B97F4A7C15ULL;
3532+ return &sp_random_default;
3533+}
3534+/* Random#bytes(n) — n random bytes as a String. Uses sp_str_set_len
3535+ so embedded NULs are preserved and #length reports n. */
3536+static const char *sp_Random_bytes(sp_Random *r, mrb_int n) {
3537+ if (n < 0) n = 0;
3538+ char *b = sp_str_alloc((size_t)n);
3539+ for (mrb_int i = 0; i < n; i++) b[i] = (char)(sp_random_next(r) & 0xff);
3540+ b[n] = 0;
3541+ sp_str_set_len(b, (size_t)n);
3542+ return b;
3543+}
3544+
35033545 /* ---- StringIO runtime ---- */
35043546 typedef struct { char *buf; int64_t len; int64_t cap; int64_t pos; int64_t lineno; int closed; } sp_StringIO;
35053547 static void sio_grow(sp_StringIO *sio, int64_t need) { int64_t req = sio->pos + need; if (req <= sio->cap) return; int64_t nc = sio->cap ? sio->cap : 64; while (nc < req) nc *= 2; sio->buf = (char *)realloc(sio->buf, nc + 1); sio->cap = nc; }