From 4cdc19b59005f629f74e74e9b23a1a4a8b2f8033 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Mon, 12 May 2025 08:03:55 +0000 Subject: [PATCH] stdlibs random for life --- src/games/life/index.ts | 2 +- src/games/life/life.c | 33 ++------------------------------- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/src/games/life/index.ts b/src/games/life/index.ts index 76b9b63..6ecfe92 100644 --- a/src/games/life/index.ts +++ b/src/games/life/index.ts @@ -12,7 +12,7 @@ const step = life.step as CallableFunction; let pixels: Uint8Array; export default function main() { - const pixelsPtr = life.initField(Date.now()); + const pixelsPtr = life.initField(); pixels = new Uint8Array(life.memory.buffer, pixelsPtr, width * height * 4); console.log(life, pixels.length); diff --git a/src/games/life/life.c b/src/games/life/life.c index 15bf45c..74289c6 100644 --- a/src/games/life/life.c +++ b/src/games/life/life.c @@ -8,7 +8,6 @@ static uint8_t* field; static uint8_t* nextField; static uint8_t* pixels; -static uint8_t rand8(void); static int countNeighbours(int x, int y); EXPORT(getWidth) int get_width(void) { return width; } @@ -66,42 +65,14 @@ static int countNeighbours(int x, int y) { return count; } -/** Random generator implementation from https://stackoverflow.com/a/16761955 */ -#define STATE_BYTES 7 -#define MULT 0x13B /* for STATE_BYTES==6 only */ -#define MULT_LO (MULT & 255) -#define MULT_HI (MULT & 256) - -static uint8_t rand_state[STATE_BYTES] = {0x87, 0xdd, 0xdc, 0x10, 0x35, 0xbc, 0x5c}; - -static uint8_t rand8(void) { - static uint16_t c = 0x42; - static uint32_t i = 0; - uint16_t t; - uint8_t x; - - x = rand_state[i]; - t = (uint16_t)x * MULT_LO + c; - c = t >> 8; -#if MULT_HI - c += x; -#endif - x = t & 255; - rand_state[i] = x; - if (++i >= sizeof(rand_state)) - i = 0; - return x; -} - -EXPORT(initField) uint8_t* init_field(uint32_t randomSeed) { - *((uint32_t*)&rand_state[STATE_BYTES - sizeof(uint32_t)]) = randomSeed; // Voodoo +EXPORT(initField) uint8_t* init_field(void) { field = malloc(width * height); nextField = malloc(width * height); pixels = malloc(width * height * 4); for (int i = 0; i < width * height; i++) { - field[i] = rand8() & 1; + field[i] = rand() & 1; } return pixels;