Discuss Scratch

Nylereds1
Scratcher
27 posts

How do you make a PRNG (Pseudo Random Number Generator)?

Hi guys I have a question:

How do I make a Pseudo Random Number Generator? It basically takes a series of numbers then generates another usually longer sequence of numbers by applying a equation to the series of numbers it was given.

Can somebody help please?
everwinner64
Scratcher
1000+ posts

How do you make a PRNG (Pseudo Random Number Generator)?

Why not use
(pick random () to (10))

As far I know it generates a pseudo random number
Nylereds1
Scratcher
27 posts

How do you make a PRNG (Pseudo Random Number Generator)?

everwinner64 wrote:

Why not use
(pick random () to (10))

As far I know it generates a pseudo random number
Yeah, but does it give the same number every time? Also, it's an RNG (Random Number Generator) not a PRNG. Also, you have to input in only 1 sequence
everwinner64
Scratcher
1000+ posts

How do you make a PRNG (Pseudo Random Number Generator)?

Well, real aleatory isn’t real in most of cases. Since something determinist can’t make aleatory things, so this block is technically a PRNG

ask [Enter a seed (a number)] and wait
set [seed v] to (answer)
ask [Enter the maximum (range)] and wait
set [max range v] to (answer)
repeat (10)
set [seed v] to ((seed) + (3))
set [seed v] to (((seed) * (7)) mod (max range))
set [random number v] to (seed)
say (random number) for (1) seconds
end
awesome-llama
Scratcher
1000+ posts

How do you make a PRNG (Pseudo Random Number Generator)?

Scratch doesn't make it very convenient to implement PRNGs or hashing as these often manipulate bits (e.g. bit shifts, XOR) and Scratch doesn't have these. Really the best operations I find are the most primitive ones… multiply, divide, add, subtract, and modulo to ensure the numbers are kept to a particular interval. There happens to be a common type of algorithm that uses these operations called an LCG. You should look towards this first. There is also a list of different PRNGs which can give you a better idea of what works. The other thing to note if you don't know this already is that prime numbers are very often used in the algorithms because their lack of factors makes for more variation in the chosen numbers.

Coming back to Scratch, a very basic example would be this:

((((seed) * (33.7469)) + (0.261447)) mod (1))

…which would take a number and output some fraction between 0 and 1.

This is what I'm using for some procedural generation in my current project. Whether this is satisfactory depends on your use case. The simplest implementations would be more likely to show visible repeating patterns. You could modify it further with more operators such as this:

((((((seed) * (609.153)) mod (1.3467)) + ((seed) * (33.7469))) + (0.261447)) mod (1))

In these examples, you can round the result if you wanted whole numbers (and also change the divisor for modulo to fit the desired range).

Last edited by awesome-llama (June 15, 2025 11:37:05)

MonkeyBean2
Scratcher
500+ posts

How do you make a PRNG (Pseudo Random Number Generator)?

Nylereds1 wrote:

everwinner64 wrote:

Why not use
(pick random () to (10))

As far I know it generates a pseudo random number
Yeah, but does it give the same number every time? Also, it's an RNG (Random Number Generator) not a PRNG. Also, you have to input in only 1 sequence
(pick random () to (10))
is a PRNG, but you cannot manually set the seed, it is set automatically based on the time or some other changing value.

Powered by DjangoBB