Commit 343baab8

343baab89c88Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Merge pull request #1404 from mattn/fix-prng-autoseed

Auto-seed the PRNG at startup so rand/shuffle vary per run

Details

Commit
343baab89c88
Parent
41d82b553a6a
Merge parent
127fd534476a

src/codegen.cmodifiedView file

@@ -2387,6 +2387,10 @@
23872387 buf_puts(&body, " sp_re_init();\n");
23882388 buf_puts(&body, " { sp_argv.len = argc - 1; sp_argv.data = (const char**)malloc(sizeof(const char*) * (size_t)(argc > 1 ? argc - 1 : 1)); for (int _ai = 0; _ai < argc - 1; _ai++) sp_argv.data[_ai] = sp_str_dup_external(argv[_ai + 1]); }\n");
23892389 buf_puts(&body, " sp_program_name = argc > 0 ? argv[0] : \"\";\n");
2390+ /* Ruby auto-seeds its PRNG at startup, so rand/shuffle/sample vary per
2391+ run. Seed once here; an explicit srand(seed) in user code runs later
2392+ and overrides this for reproducible sequences. */
2393+ buf_puts(&body, " srand((unsigned)time(NULL));\n");
23902394 /* Register END blocks (atexit runs LIFO, so they execute in reverse registration order) */
23912395 for (int e = 1; e <= end_count; e++)
23922396 buf_printf(&body, " atexit(sp_end_fn_%d);\n", e);