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

#1 2010-06-22 18:02:13

Merck82ABN
Scratcher
Registered: 2010-06-02
Posts: 6

Randomizing events

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

 

#2 2010-06-22 18:09:10

soupoftomato
Scratcher
Registered: 2009-07-18
Posts: 1000+

Re: Randomizing events

Eenin meeni mini moe XD JK

I really don't know, that's interesting.  hmm

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.


I'm glad to think that the community will always be kind and helpful, the language will always be a fun and easy way to be introduced into programming, the motto will always be: Imagine, Program, Share - Nomolos

Offline

 

#3 2010-06-22 18:12:35

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Randomizing events

I can't exactly describe the process, but here are the two Squeak methods:
initialize

Code:

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

Code:

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].
    ^ result

nXIII

Offline

 

#4 2010-06-23 18:09:48

Merck82ABN
Scratcher
Registered: 2010-06-02
Posts: 6

Re: Randomizing events

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

 

#5 2010-06-23 20:31:22

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Randomizing events

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)


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#6 2010-06-25 15:10:54

Merck82ABN
Scratcher
Registered: 2010-06-02
Posts: 6

Re: Randomizing events

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

Offline

 

Board footer