Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make a random number based on a seed?
- Williamja
-
65 posts
How to make a random number based on a seed?
Everyone knows about the (random number from (something) to (something)) block, but how do you ask someone for a number and make a bunch of random numbers from (something) to (something) based on it! (Example: If I enter 22938 as the number and ask for numbers from 20 to 200 and get 109, 75, 195, 34, I should be able to give the computer the same number and get 109, 75, 195, 34, again.
- drmcw
-
1000+ posts
How to make a random number based on a seed?
Try this. There are plenty of such algorithms but they tend to use bit shifts and xors which Scratch doesn't have built-in, so you'd have to write, but should be possible.It would also help knowing what you want it for as the algorithms all have various advantages and disadvantages.
Last edited by drmcw (July 15, 2013 15:34:16)
- DadOfMrLog
-
1000+ posts
How to make a random number based on a seed?
I invented a pseudo-random-sequencer a few years back that generates numbers in a random-looking sequence from about -2^32 to +2^32 (excluding zero). If you give it a number (not zero) then it generates the next one in the sequence. If you give it zero then it generates the next one in the sequence from the last one it generated.
You can easily use it as the basis for a PseudoRNG, just by doing the returned number modulo some range you want, plus the minimum value you want.
I've got it in a custom block for an encryption project (that I've never shared), so it's all ready to go…
If you want to generate loads of such numbers, then it's not going to be anywhere near as fast as the built-in “pick random…” (the block has to do a few multiplies/adds/modulos, and scratch is pretty slow at interpreting script), but it'll be fine if speed isn't that much of a concern.
Let me know if you want to try it…
You can easily use it as the basis for a PseudoRNG, just by doing the returned number modulo some range you want, plus the minimum value you want.
I've got it in a custom block for an encryption project (that I've never shared), so it's all ready to go…
If you want to generate loads of such numbers, then it's not going to be anywhere near as fast as the built-in “pick random…” (the block has to do a few multiplies/adds/modulos, and scratch is pretty slow at interpreting script), but it'll be fine if speed isn't that much of a concern.
Let me know if you want to try it…
- Williamja
-
65 posts
How to make a random number based on a seed?
Oh I would really wanna try it please give me the script!!! I invented a pseudo-random-sequencer a few years back that generates numbers in a random-looking sequence from about -2^32 to +2^32 (excluding zero). If you give it a number (not zero) then it generates the next one in the sequence. If you give it zero then it generates the next one in the sequence from the last one it generated.
You can easily use it as the basis for a PseudoRNG, just by doing the returned number modulo some range you want, plus the minimum value you want.
I've got it in a custom block for an encryption project (that I've never shared), so it's all ready to go…
If you want to generate loads of such numbers, then it's not going to be anywhere near as fast as the built-in “pick random…” (the block has to do a few multiplies/adds/modulos, and scratch is pretty slow at interpreting script), but it'll be fine if speed isn't that much of a concern.
Let me know if you want to try it…

- DadOfMrLog
-
1000+ posts
How to make a random number based on a seed?
@Williamja:
Just shared it. Added a couple of custom blocks to help use it for what you want.
Let me know if you have any questions about it…
Just shared it. Added a couple of custom blocks to help use it for what you want.
Let me know if you have any questions about it…
- Williamja
-
65 posts
How to make a random number based on a seed?
Thanks! This is just what I needed! @Williamja:
Just shared it. Added a couple of custom blocks to help use it for what you want.
Let me know if you have any questions about it…

- DadOfMrLog
-
1000+ posts
How to make a random number based on a seed?
I invented a pseudo-random-sequencer a few years back that generates numbers in a random-looking sequence from about -2^32 to +2^32 (excluding zero)…
Just realised I can't count

It's actually -2147483645 to +2147483645 (as the project notes say).
(2^31=2147483648, so three more either side than range above.)
Anyway, glad you find it useful.

Last edited by DadOfMrLog (July 16, 2013 14:43:12)
- suikun
-
15 posts
How to make a random number based on a seed?
How would I use seeds in a Project?

- MoreGamesNow
-
100+ posts
How to make a random number based on a seed?
A very simple and quite fast pseudo-random number generator is this:
These numbers are certainly not cryptographically secure (and the distribution is not exactly uniform), but may still be serviceable for you (certainly it is fairly easy to implement!).
The most significant flaw in this is that if the number “0” is ever added to the list, all subsequent numbers will also be zero. For instance if the first inputted number is 355 this will simply generate only zeroes. A simple (though perhaps inelegant) solution would be:
Another possible flaw (if this is significant) is that the numbers are not uniformly random.
These numbers are certainly not cryptographically secure (and the distribution is not exactly uniform), but may still be serviceable for you (certainly it is fairly easy to implement!).
The most significant flaw in this is that if the number “0” is ever added to the list, all subsequent numbers will also be zero. For instance if the first inputted number is 355 this will simply generate only zeroes. A simple (though perhaps inelegant) solution would be:
Another possible flaw (if this is significant) is that the numbers are not uniformly random.
- deck26
-
1000+ posts
How to make a random number based on a seed?
Please don't spam or necropost. Respect the forum, you may need it one day.
- Benkimbow
-
15 posts
How to make a random number based on a seed?
This is not uniform its completly broken and it can get byist on a number poositive or negative.
- deck26
-
1000+ posts
How to make a random number based on a seed?
Not sure it's worth necroposting to say that. If you have a question crreate a new topic - you can always refer to this topic if necessary. This is not uniform its completly broken and it can get byist on a number poositive or negative.
- bsteichman
-
500+ posts
How to make a random number based on a seed?
A very simple and quite fast pseudo-random number generator is this:
These numbers are certainly not cryptographically secure (and the distribution is not exactly uniform), but may still be serviceable for you (certainly it is fairly easy to implement!).
The most significant flaw in this is that if the number “0” is ever added to the list, all subsequent numbers will also be zero. For instance if the first inputted number is 355 this will simply generate only zeroes. A simple (though perhaps inelegant) solution would be:
Another possible flaw (if this is significant) is that the numbers are not uniformly random.
i have a question about this, doesn't sin repeat?, i need it so it never has the same order of numbers again
i need this so i can create a seed based noise to implement into terrain generation.
- bsteichman
-
500+ posts
How to make a random number based on a seed?
i didnt mean to press save here and idk how to delete posts
Last edited by bsteichman (Dec. 3, 2020 14:15:13)
- MDCCCLXVII
-
1000+ posts
How to make a random number based on a seed?

Please create a new topic if you need help with your Scratch project.
- ILikeScratch0-0-0-1
-
27 posts
How to make a random number based on a seed?
On khan academy, in computer science, you get to know about pseudo-random generated numbers based on a seed, it kinda goes like this:
So repeat those steps:
1. Pick a seed
2. Multiply the seed by itself
3. Turn it into a string and get the numbers in the middle that has the same numbers of digits in the seed
4. Set to output to itself and the output
5. Set the next seed to the output
So in scratch, it is like this:
Tested and worked
Hopes it helps your problems
Your best helper, ILikeScratch0-0-0-1
So repeat those steps:
1. Pick a seed
2. Multiply the seed by itself
3. Turn it into a string and get the numbers in the middle that has the same numbers of digits in the seed
4. Set to output to itself and the output
5. Set the next seed to the output
So in scratch, it is like this:
Tested and worked
Hopes it helps your problems
Your best helper, ILikeScratch0-0-0-1

Last edited by ILikeScratch0-0-0-1 (Dec. 10, 2020 04:49:25)
- ILikeScratch0-0-0-1
-
27 posts
How to make a random number based on a seed?
Pseudo-random numbers can be used in all kinds of stuff, and my favorite is codes
- ILikeScratch0-0-0-1
-
27 posts
How to make a random number based on a seed?
A very simple and quite fast pseudo-random number generator is this:
These numbers are certainly not cryptographically secure (and the distribution is not exactly uniform), but may still be serviceable for you (certainly it is fairly easy to implement!).
The most significant flaw in this is that if the number “0” is ever added to the list, all subsequent numbers will also be zero. For instance if the first inputted number is 355 this will simply generate only zeroes. A simple (though perhaps inelegant) solution would be:
Another possible flaw (if this is significant) is that the numbers are not uniformly random.
Nope, this pseudo-random number generator don't works

If your output had many zeros after it, then I think you needs to check your code

Last edited by ILikeScratch0-0-0-1 (Dec. 10, 2020 04:58:21)
- ILikeScratch0-0-0-1
-
27 posts
How to make a random number based on a seed?
khan academy, in computer science, you get to know about pseudo-random generated numbers based on a seed, it kinda goes like this:On
So repeat those steps:
1. Pick a seed
2. Multiply the seed by itself
3. Turn it into a string and get the numbers in the middle that has the same numbers of digits in the seed
4. Set to output to itself and the output
5. Set the next seed to the output
So in scratch, it is like this:
Tested and worked
Hopes it helps your problems
Your best helper, ILikeScratch0-0-0-1
So yeah, never feed it large numbers or it will be like77438805e+2219828026e+5905667603e+…
Blah blah blah, it became awkward. D:,

My project for it: https://scratch.mit.edu/projects/461631288/
Last edited by ILikeScratch0-0-0-1 (Dec. 10, 2020 05:04:30)
- Discussion Forums
- » Help with Scripts
-
» How to make a random number based on a seed?