On Fri, Oct 09, 2026 at 09:14:08PM -0400, Story said:
Hello openbsd users and developers. I read this exchange between these two 
gentlemen on ports@, and I have been building a tile game where each tile is 
one byte, and I want to use this function to generate a random page for the map:

#include <stdlib.h>

int main(void) {
   unsigned char page[4096];
   arc4random_buf(page, sizeof(page));
   return 0;
}

As others have well-described the concerns Theo raised about the use of rand(3) I wanted to mention that in the context of your use-case (game programming) you may absolutely *require* a deterministic pseudo-random number generator. This is, for example the sort of behavior relied upon by most games that use a seed to generate a random world. If you wish to be able to have yor random map be re-created (whether for play or testing) then you need a deterministic algorithm.

Famously, Doom used a static table[1] as its PRNG source so that it could record gameplay in demo files by simply recording a stream of input events and timing information. By initalizing the PRNG the same way as the original session, the demo playback can just replay the input events and the game engine will play itself as the original player did.

Using arc4random(3) or similar cryptographic pseudo-random number generators will provide you with quality random numbers but may complicate other aspects of your game.

1: https://github.com/id-Software/DOOM/blob/master/linuxdoom-1.10/m_random.c

--Matt

--
Please direct replies to the list.

Reply via email to