This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-12-02 14:31:20

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

How does Scratch randomly select a number?

I mean, if you think about it, a computer only does what it's told to, so will be unable to do anything unexpected. So does this mean the 'Pick random # to #' block isn't COMPLETELY random? I can't really see how it can...


Why

Offline

 

#2 2011-12-02 14:36:47

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: How does Scratch randomly select a number?

Without looking at the source code to Scratch, I'll take a guess and say they are using the Linear Congruential Method of selecting random sequences.

http://en.wikipedia.org/wiki/Linear_con … _generator

Enjoy.


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#3 2011-12-02 14:44:10

Lar-Rew
Scratcher
Registered: 2010-02-19
Posts: 100+

Re: How does Scratch randomly select a number?

x^large prime mod another large prime is quite good at generating random numbers.  It might be doing some modification of this.

Offline

 

#4 2011-12-02 14:47:36

my-chemical-romance
Scratcher
Registered: 2011-04-24
Posts: 1000+

Re: How does Scratch randomly select a number?

You're telling it to get a random number within a range, it's not 100% random.

Offline

 

#5 2011-12-02 14:47:44

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How does Scratch randomly select a number?

BoltBait wrote:

Without looking at the source code to Scratch, I'll take a guess and say they are using the Linear Congruential Method of selecting random sequences.

http://en.wikipedia.org/wiki/Linear_con … _generator

Enjoy.

Thanks for replying, but I don't really understand anything Wikipedia says about maths!  tongue


Why

Offline

 

#6 2011-12-02 15:18:53

Dinoclor
Scratcher
Registered: 2010-06-10
Posts: 1000+

Re: How does Scratch randomly select a number?

It probably uses rand, which gets it's random number from the time, making it actually pseudorandom.


This is a temporary signature. It will exist until I think of something witty.

Offline

 

#7 2011-12-02 15:58:28

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: How does Scratch randomly select a number?

Scratch indeed uses a pseudo-random number generator, based on the run time of the VM (how long Scratch has been running for).

It seeds the milliseconds from the run time to obtain the number you asked for in your 'pick random' block. It is not truly random, and the sequence will eventually repeat, but no project will ever run long enough to reach the 4 294 967 296 repetitions!  tongue  (that's 2^32)

To get a truly random factor you would need to use something physical such as the temperature (which, again, does not vary much) or something else from the exterior.

That was explained in really basic terms and, as a consequence, not very appropriate vocabulary. I could slightly expand on that but I don't think there's the need, and if you really do want to know more there's always that wikipedia page on PRNG's  wink

Last edited by LS97 (2011-12-02 16:00:17)

Offline

 

#8 2011-12-02 16:05:53

RedRocker227
Scratcher
Registered: 2011-10-26
Posts: 1000+

Re: How does Scratch randomly select a number?

LS97 wrote:

Scratch indeed uses a pseudo-random number generator, based on the run time of the VM (how long Scratch has been running for).

It seeds the milliseconds from the run time to obtain the number you asked for in your 'pick random' block. It is not truly random, and the sequence will eventually repeat, but no project will ever run long enough to reach the 4 294 967 296 repetitions!  tongue  (that's 2^32)

To get a truly random factor you would need to use something physical such as the temperature (which, again, does not vary much) or something else from the exterior.

That was explained in really basic terms and, as a consequence, not very appropriate vocabulary. I could slightly expand on that but I don't think there's the need, and if you really do want to know more there's always that wikipedia page on PRNG's  wink

Thanks a lot, this was the best answer by far.  smile


Why

Offline

 

Board footer