Discuss Scratch

Blue_Moon2607
Scratcher
63 posts

Pseudo Random Number Generator (PRNG) in Scratch

This is a tutorial on making a Linear Congruential Generator (LCG) PRNG in Scratch to generate “seeds”.
Want to generate a random number from a seed? It is as easy as 1, 2, 3! You will only need 20 blocks (or 5) and 2 variables to generate a random number from the PRNG algorithm.

The algorithm is a Linear Congruential Generator, and it has been modified to extend the randomness. I added the repeat block to continuously “randomise” the result.

Keep in mind that blank spaces are the number you can randomly put below…
For the unmodified LCG algorithm, here is the block:
set [PRNG Result v] to ((((PRNG Seed) * ()) + ()) mod ())
Make sure that the modulor is bigger than your desired seed range limit.

For the modified, “randomness”-extended LCG algorithm (which I've mentioned), here is the block:
set [PRNG Result v] to ((((PRNG Seed) * ()) + ()) mod ())
repeat ((((PRNG Result) + (PRNG Seed)) mod ()) * (((PRNG Result) - (PRNG Seed)) mod ()))
set [PRNG Result v] to ((((PRNG Seed) * ()) + ()) mod ())
end
Make sure the modulor in the repeat block is not too big (make it 15 - 20 for no “Run without screen refresh” or 300 - 600 for “Run without screen refresh”) to lag the project or cause a huge delay. You can wrap it in a Custom Block and turn on “Run without screen refresh” to instantly execute it and not be bound to the project's time frame.

This algorithm is used by SEED World Generation, though it's a little bit different. It can also be used on other projects that exist on the Scratch website.

Last edited by Blue_Moon2607 (Oct. 9, 2025 16:54:58)

Powered by DjangoBB