Log In  


We can set seed to any value. But we can't reset it back so it would be random. Could you add this feature?

3


Not sure I understand?

it's random.

you can seed to control (or not) the values. they are still random.

the seed is so that, for example, you can use the same set of random values if need arises.

or...what do you want exactly?


If you don't use srand, it gets called in the background with a random seed when you boot the cart, so all random calls are different each run. If you do use srand <seed>, all the next calls to rnd will come out of the same table, so you get the same results, in the same order, every time you run the cart.

If you want to for example use the srand seed to generate a map, but then want to go back to unpredictable things for other events (wind, hit chance etc), you need to give it a random seed.
Usually you call srand using the current time as the seed (or NULL, giving you random memory garbage) but with pico-8 that isn't possible.

You could try using a bunch of other stuff as the seed, like stat, btn, grabbing some memory locations, using the global T time variable, chaining rnd calls to it and so on. Do this constantly on the background and you get a fairly good chance you won't get two same results.


2

I ran into this problem and came up with a quick solution.

1) Store rand() into a global var trand.
2) Set srand() to a fixed number.
3) After running deterministic section, set srand(trand).


Your best source of entropy for a 'random' number to seed with will be player btn() presses.


1

I use innomin's method (to avoid providing an extra api function); the seed is random on cart boot for this reason, and is based on mysterious system-specific state and the phase of the moon.



[Please log in to post a comment]