Discuss Scratch

Williamja
Scratcher
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
Scratcher
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
Scratcher
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…
Williamja
Scratcher
65 posts

How to make a random number based on a seed?

DadOfMrLog wrote:

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…
Oh I would really wanna try it please give me the script!!!
DadOfMrLog
Scratcher
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…
Williamja
Scratcher
65 posts

How to make a random number based on a seed?

DadOfMrLog wrote:

@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…
Thanks! This is just what I needed!
DadOfMrLog
Scratcher
1000+ posts

How to make a random number based on a seed?

DadOfMrLog wrote:

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 -should've been “about -2^31 to +2^31”.
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
Scratcher
15 posts

How to make a random number based on a seed?

How would I use seeds in a Project?
MoreGamesNow
Scratcher
100+ posts

How to make a random number based on a seed?

A very simple and quite fast pseudo-random number generator is this:

addseedtolistrepeat100addroundsinofitemlastoflist*1000tolist generate integers from -1000 to 1000delete1oflist remove the original seed

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:

addseedtolistrepeat100ifitemlastoflist=0add5tolist add some random value if it is zeroelseaddroundsinofitemlastoflist*1000tolistdelete1oflist

Another possible flaw (if this is significant) is that the numbers are not uniformly random.
GBstudio
Scratcher
13 posts

How to make a random number based on a seed?

ifonedge,DIE!!
deck26
Scratcher
1000+ posts

How to make a random number based on a seed?

GBstudio wrote:

ifonedge,DIE!!
Please don't spam or necropost. Respect the forum, you may need it one day.
Benkimbow
Scratcher
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
Scratcher
1000+ posts

How to make a random number based on a seed?

Benkimbow wrote:

This is not uniform its completly broken and it can get byist on a number poositive or negative.
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.
bsteichman
Scratcher
500+ posts

How to make a random number based on a seed?

MoreGamesNow wrote:

A very simple and quite fast pseudo-random number generator is this:

addseedtolistrepeat100addroundsinofitemlastoflist*1000tolist generate integers from -1000 to 1000delete1oflist remove the original seed

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:

addseedtolistrepeat100ifitemlastoflist=0add5tolist add some random value if it is zeroelseaddroundsinofitemlastoflist*1000tolistdelete1oflist

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
Scratcher
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
Scratcher
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
Scratcher
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:

definegeneratepseudo-randomnumbersfromseedSEEDsetseedtoSEEDdeleteallofoutputforeversetseed^2toseed*seedsetls2tolengthofseed^2setnext seed starting postoroundls2-3/2setnext seedtosetito1repeatnextseedstartingposchangeiby1repeatlengthofSEEDsetnext seedtojoinnextseedletteriofseed^2changeiby1addnextseedtooutputsetseedtonextseed



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
Scratcher
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
Scratcher
27 posts

How to make a random number based on a seed?

MoreGamesNow wrote:

A very simple and quite fast pseudo-random number generator is this:

addseedtolistrepeat100addroundsinofitemlastoflist*1000tolist generate integers from -1000 to 1000delete1oflist remove the original seed

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:

addseedtolistrepeat100ifitemlastoflist=0add5tolist add some random value if it is zeroelseaddroundsinofitemlastoflist*1000tolistdelete1oflist

Another possible flaw (if this is significant) is that the numbers are not uniformly random.

Nope, this pseudo-random number generator don't works . A pseudo-random generation will never make 0 into some hard-coded number. After it was zero, then it skips the number and continue the next set of numbers, and will leave the zero there. No matter what.

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
Scratcher
27 posts

How to make a random number based on a seed?

ILikeScratch0-0-0-1 wrote:

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:

definegeneratepseudo-randomnumbersfromseedSEEDsetseedtoSEEDdeleteallofoutputforeversetseed^2toseed*seedsetls2tolengthofseed^2setnext seed starting postoroundls2-3/2setnext seedtosetito1repeatnextseedstartingposchangeiby1repeatlengthofSEEDsetnext seedtojoinnextseedletteriofseed^2changeiby1addnextseedtooutputsetseedtonextseed



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)

Powered by DjangoBB