From 7683c065919d1f7d21a6dc47e7c71268762a5c0a Mon Sep 17 00:00:00 2001 From: phfr24 Date: Mon, 17 Nov 2025 14:48:47 -0300 Subject: [PATCH] use xorshift instead of rand() --- prototype.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/prototype.c b/prototype.c index 2e9ef3f..83b9398 100644 --- a/prototype.c +++ b/prototype.c @@ -1,8 +1,7 @@ +#include #include -#include #include #include -#include #define COLS 80 #define ROWS 25 @@ -15,6 +14,18 @@ #define STR(x) _STR (x) #define _STR(x) #x +static uint16_t xss; /* xs() state */ + +static uint16_t +xs () /* xorshift prng */ +{ + xss ^= (xss << 1); + xss ^= (xss >> 3); + xss ^= (xss << 10); + + return xss; +} + static void clearscr () { @@ -39,7 +50,7 @@ init_grid (char grid[COLS * ROWS]) { for (int i = 0; i < COLS * ROWS; i++) { - if (rand () % INIT_RATIO) + if (xs () % INIT_RATIO) grid[i] = DEAD; else grid[i] = ALIVE; @@ -108,7 +119,7 @@ main () { char grid[COLS * ROWS]; - srand (time (0)); + xss = time (0); init_grid (grid); do -- 2.54.0