Random seed

Inspired by @shakao’s awesome city skyline generator, I started playing around with a procedurally generated art project of my own.

As I’m writing it, I’m finding I want to be able to seed the random number generator with a specific value. Doing so would a) let me see a consistent result as I develop and b) let specific outcomes be more sharable if the seed was known…

I know JS’s Math.random doesn’t expose any seeded random support (yet… Stage 1 proposal), but I figured it’s possible you’ve already written your own random implementation anyway…

Is it technically possible today to seed the random number generator with a specific value? Do you think adding a block to support that would be useful?

1 Like

https://github.com/microsoft/pxt-common-packages/pull/465 fastrandom has that (though I believe at this point the class name is incredibly misleading, as it’s likely slower now than Math.random …) - I believe it’s still used in particle effects, but I may have switched them at some point. Worth noting it’s got a range of 0x0000 to 0xffff, if thats a deal breaker

I feel like I might’ve seen a PR re: adding the ability to seed random at some point but I’m not sure - it would probably have been mmoskal and would have been in pxt and/or pxt-common-packages (I’m on my phone, can look for it later).

If you want it for actually seeding things, you probably want the object oriented approach anyway - to keep things mostly consistent (avoid timing issues or things of the sort causing different behavior) you would likely need a different seeded random for each event / sprite / anything of the sort.

You might be thinking of this https://github.com/microsoft/pxt-common-packages/pull/908 which didn’t get merged in.

It definitely feels useful to me, for both reasons you mentioned as well as in an classroom context where a teacher might want to sync their students to the same result. I think it’s at least worth submitting an issue for, to keep it on the radar when we get back to Arcade features!

2 Likes

it’s at least worth submitting an issue for

Will do