Log In  

I keep running into scenarios where knowing what algorithm pico-8 uses for random number generation would come in handy. Does anyone know what it is or how to get that info? Thanks!

P#124120 2023-01-12 05:51

3

Here's a re-implementation of PICO-8's RNG in PICO-8. Of note, the last line of rng.rnd is just returning rng.hi%n but with some modulo arithmetic to account for within-PICO-8-overflow and be 1:1 with calling the built-in rnd.

function init_rng(seed)
 local rng={}
 function rng.srand(seed)
  seed=seed or 0
  rng.hi,rng.lo=
   seed==0 and 0x6000.9755 or seed^^0xbead.29ba,
   seed==0 and 0xdead.beef or seed
  for i=1,0x20 do rng.rnd() end
 end
 function rng.rnd(n)
  n=n or 1
  rng.hi=(rng.hi<<0x10|rng.hi>>>0x10)+rng.lo
  rng.lo+=rng.hi
  return ((rng.hi<0 and 0x8000-n or 0)%n+(rng.hi&0x7fff.ffff)%n)%n
 end
 rng.srand(seed)
 return rng
end
P#124122 2023-01-12 06:14 ( Edited 2023-01-12 21:26)

-- code removed at the request of @merwok.

Thanks for being on top of this !

P#124123 2023-01-12 06:33 ( Edited 2023-01-12 18:51)
1

that’s not relevant to OP’s question

P#124148 2023-01-12 15:45
 
P#124156 [hidden by admin]

While I'm not 100% certain, PICO-8 does not seem to be using Lua's in-built PRNG. Doing a test with Lua's math.random() function and PICO-8's rnd() function, both with the same seed, gives different results. I know that doesn't give you a complete answer, but at least eliminates one possibility. :)

P#124226 2023-01-13 16:33

[Please log in to post a comment]