Discuss Scratch
- Discussion Forums
- » Questions about Scratch
- » Scratch Random Number Generator Formula
- Mr-Games
-
Scratcher
39 posts
Scratch Random Number Generator Formula
Hello!
Recently I was curious on what formula Scratch uses for generating random numbers, so I asked @Za-Chary*, and he said to check the forums. After some unsuccessful searching I couldn't find any answers. If you know, or have suggestions on who might know, or where I could look that would be awesome.
Happy Scratching!
@Mr-Games












Recently I was curious on what formula Scratch uses for generating random numbers, so I asked @Za-Chary*, and he said to check the forums. After some unsuccessful searching I couldn't find any answers. If you know, or have suggestions on who might know, or where I could look that would be awesome.
Happy Scratching!
@Mr-Games












Last edited by Mr-Games (July 9, 2020 00:02:37)
- Basic88
-
Scratcher
1000+ posts
Scratch Random Number Generator Formula
I'm sure the computer just thinks “Random Number between (foo) and (foo)? I'll just pick the first apparent number that comes to mind”. It's just the way the Computer works.
- Mr-Games
-
Scratcher
39 posts
Scratch Random Number Generator Formula
I'm sure the computer just thinks “Random Number between (foo) and (foo)? I'll just pick the first apparent number that comes to mind”. It's just the way the Computer works.
Yeah, but isn't there a formula for how it generates the number?
- Basic88
-
Scratcher
1000+ posts
Scratch Random Number Generator Formula
Yeah, but isn't there a formula for how it generates the number?Yes, but that's a lot of complicated math.
Fun Fact:
The numbers given with this block are not truly random — they are merely unpredictable. It is nearly impossible to generate truly random numbers using a computer.
- Abigblueworld
-
Scratcher
500+ posts
Scratch Random Number Generator Formula
Hello!Number layout:
Recently I was curious on what formula Scratch uses for generating random numbers, so I asked @Za-Chary*, and he said to check the forums. After some unsuccessful searching I couldn't find any answers. If you know, or have suggestions on who might know, or where I could look that would be awesome.
Happy Scratching!
@Mr-Games
1-2-3-4-5-6-7-8-9-10,
Side number layout
11-12-13-14-15-16-17-18-19-20
For the random number to happen, the computer has to do short math, the numbers here 10, 20 are not seen
For 20 in random, 11 + 9.
This won’t do it.
4+17=uh oh, it’s 21.
So the math here will use 1-28 because, 9 + 19 = 28
So that’s 1-28, the computer has to do the math…
For 1-3
1 2
1 2
It has to do 1-2 or 1+2. Or 3-2.
Sorry if this ain’t true.
- Mr-Games
-
Scratcher
39 posts
Scratch Random Number Generator Formula
Thanks!Yeah, but isn't there a formula for how it generates the number?Yes, but that's a lot of complicated math.
Fun Fact:The numbers given with this block are not truly random — they are merely unpredictable. It is nearly impossible to generate truly random numbers using a computer.
- ioton
-
Scratcher
500+ posts
Scratch Random Number Generator Formula
Math.round(Math.random() * (max - min) + min)
function randomNum(min = 0, max = 0) {
return Math.round(Math.random() * (max - min) + min)
}
// the line of code below will result in a random number between 2 and 4 being generated and logged to the console if the function is defined.
console.log(randomNum(2, 4))- hedgehog_blue
-
Scratcher
1000+ posts
Scratch Random Number Generator Formula
The math to actually get the random part is complex, but the main details you need to know is that the probability of each output is equal (although when using decimal values the probability of each result is 0, so in that case it would be better described as the probability of any 2 ranges of equal size within the random range having equal probabilities).
If the inputs are both integers, it returns an integer between or equal to the inputs. If any of them are decimal, it returns a random decimal amount between the inputs or equal to the lower bound of the range (the upper bound will never be the result, although that doesn't matter too much because the decimal amounts are theoretically infinitely precise, meaning the result can be infinitesimally close to the upper bound).
This is how it would look:
Here's some other details that aren't really necessary to include but it is more accurate to how scratch works.
Scratch also does not round if any of the inputs are not integers, so you would want to add this case:
It also has support for putting the range in descending order like this:
Of course, your version still works to get random numbers, this just gets all of the details of exactly how scratch gets the value.
Edit: This basically matches up with the official code for the pick random block
If the inputs are both integers, it returns an integer between or equal to the inputs. If any of them are decimal, it returns a random decimal amount between the inputs or equal to the lower bound of the range (the upper bound will never be the result, although that doesn't matter too much because the decimal amounts are theoretically infinitely precise, meaning the result can be infinitesimally close to the upper bound).
The general concept is correct, but the details don't quite match up with the way scratch chooses random numbers. Because you are rounding the result to the nearest integer, you end up with a 25% chance of 2, a 50% chance of 3, and a 25% chance of 4. However, scratch has an equal probability of each result, so it would be good to round down, and to add 1 to the max because the range is inclusive of the maximum.Also, as a function:Math.round(Math.random() * (max - min) + min)function randomNum(min = 0, max = 0) {
return Math.round(Math.random() * (max - min) + min)
}
// the line of code below will result in a random number between 2 and 4 being generated and logged to the console if the function is defined.
console.log(randomNum(2, 4))
This is how it would look:
function randomNum(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }
Here's some other details that aren't really necessary to include but it is more accurate to how scratch works.
Scratch also does not round if any of the inputs are not integers, so you would want to add this case:
function randomNum(min, max) { if(Math.round(min) === min && Math.round(max) === max) { return Math.floor(Math.random() * (max - min + 1) + min); } else { return Math.random() * (max - min) + min; } }
(pick random (10) to (1))So add that in as well:
function randomNum(min, max) { let realMin = Math.min(min, max); let realMax = Math.max(min, max); if(Math.round(min) === min && Math.round(max) === max) { return Math.floor(Math.random() * (realMax - realMin + 1) + realMin); } else { return Math.random() * (realMax - realMin) + realMin; } }
Edit: This basically matches up with the official code for the pick random block
Last edited by hedgehog_blue (July 9, 2020 18:51:04)
- SlenTheOoof
-
Scratcher
93 posts
Scratch Random Number Generator Formula
Hello!
Recently I was curious on what formula Scratch uses for generating random numbers, so I asked @Za-Chary*, and he said to check the forums. After some unsuccessful searching I couldn't find any answers. If you know, or have suggestions on who might know, or where I could look that would be awesome.
Happy Scratching!
@Mr-Games
The way it works is it makes ‘psuedorandom’ numbers that are generated using large numbers, and you can find projects on scratch that make use of them
- Mr-Games
-
Scratcher
39 posts
Scratch Random Number Generator Formula
Thanks!Hello!
Recently I was curious on what formula Scratch uses for generating random numbers, so I asked @Za-Chary*, and he said to check the forums. After some unsuccessful searching I couldn't find any answers. If you know, or have suggestions on who might know, or where I could look that would be awesome.
Happy Scratching!
@Mr-Games
The way it works is it makes ‘psuedorandom’ numbers that are generated using large numbers, and you can find projects on scratch that make use of them
- ScratchCatHELLO
-
Scratcher
1000+ posts
Scratch Random Number Generator Formula
This looks like it belongs in advanced topics, but it's resolved, so I won't move it.
- Discussion Forums
- » Questions about Scratch
-
» Scratch Random Number Generator Formula
