I am curious about how Scratch goes about aquiring/generating random numbers?
I have noticed what appears to be some uses of the "random" blocks, but they don't always apear to be truly random.
Does Scratch utilize the "timer" to "seed" the random number generator?
Say if I were to generate one of two choices by using Random 0 to 1. How does Scratch actually goe about choosing the number?
Offline
Eenin meeni mini moe XD JK
I really don't know, that's interesting.
All I know, is in things where I want a random word or something, it picks the same number over and over again then switches.
Offline
I can't exactly describe the process, but here are the two Squeak methods:
initialize
initialize
"Pick a non-zero starting seed based on the clock and this objects's hash bits."
seed _ (Time millisecondClockValue bitXor: self hash) bitXor: Time totalSeconds.
"if seed is zero, get some random bits a different way"
seed = 0 ifTrue: [seed _ Smalltalk primBytesLeft + 42].
seed _ seed abs bitAnd: 16r3FFFFFFF.
a _ 16807 asFloat.
m _ 2147483647 asFloat.
q _ (m quo: a) asFloat.
r _ (m \\ a) asFloat.
self next. "take one step"and nextValue
nextValue
"This method generates random instances of Integer in the interval 0 to 16r7FFFFFFF. This method does NOT update the seed; repeated sends answer the same value.
The algorithm is described in detail in 'Random Number Generators:
Good Ones Are Hard to Find' by Stephen K. Park and Keith W. Miller
(Comm. Asso. Comp. Mach., 31(10):1192--1201, 1988)."
| lo hi aLoRHi result |
hi _ (seed quo: q) asFloat.
lo _ seed - (hi * q). "= seed rem: q"
aLoRHi _ (a * lo) - (r * hi).
result _ (aLoRHi > 0.0)
ifTrue: [aLoRHi]
ifFalse: [aLoRHi + m].
^ resultOffline
I don't know if we can impliment anything like above in Scratch?
This is why I was curious about how (or what) Scratch uses to "seed" the random number.
I have tried resetting the TIMER each time I run a random generating program, to see if the items appear close to each other (since always starting from zero), but there appears to be changes each time. This may just be from Scratch using part of the timer (say to the thousandths decimal), but the display only shows one digit after the decimal.
There is one project that "tends to fill towards the right", which if the program was random it should jump around and not "tend" to any one spot.
I think I will set up a project that selects random items and fills a table and let it run for a long time, and try to see if it is indeed "leaning" one way or the other.
Thanks for the ideas!
Offline
AddZero (or Jtxt, as we must now refer to him), has an interesting project which creates psuedorandom numbers based off of a selectable seed:
http://scratch.mit.edu/projects/AddZero/175477
Last edited by fullmoon (2010-06-23 20:31:31)

Offline
Thanks for the link to the project. I will look it over and see if it answers some of my questions.
Offline