#Random: help me understand it?

1 messages · Page 1 of 1 (latest)

hasty zenith
#

Hello, I am trying to use std.rand to generate a random number to randomise the position of a sprite. The problem is that the random number is generated only once and then the number is kept forever until I recompile the application changing the code. What am I doing wrong?

var rand_impl = std.rand.DefaultPrng.init(0);
var num = rand_impl.random().intRangeAtMost(i32, 120, WINDOW_HEIGHT - 120);
currentDrone.dest.y = num;

I am sure I am missing something.

crimson drift
#

you have to keep the rand_impl object around somewhere

#

then every time you get a number it will be different

#

if you initialize it every time with the same seed it will give the same number

hasty zenith
#

Oh silly me thats why! Thank you so much for the help

maiden umbra
#

If you want the numbers to be different each run, you should set a random seed. Current time + process ID is a good fast option, or you can just use std.os.getrandom, which is a bit slow but if you're only running it once it's fine