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

#1 2007-07-17 04:37:07

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

How random is random?

When I listened to my random music project (http://scratch.mit.edu/projects/Jens/13629) in the offline version I unexpectedly found myself actually humming along from time to time. This surprised me, because using random variables is supposed to lead to an unpredictable course of the project. Yet, in this project (at least in the offline version) the exact same patterns and musical sequences keep coming up from time to time.

One thing I noticed is that when I execute the project right after opening Scratch with it, it will virtually always start off with the exact same set of variables, even though the values are supposedly set completely at random. Am I missing something here?

In the online (java) version I did not (yet) notice any sequence-patterns.

Is this perhaps a Squeak problem? I noticed something similar, when I wrote a sudoku generator in Squeak (http://map.squeak.org/package/e2edc767-b26d-4180-8c2c-cff955ea249e): Even though the grids and the givens are set at random the same patterns keep getting generated in the exact same sequence (albeit not always, but often enough to be annoying).

Is there anything I can do to avoid this problem or anything a can contribute to its solution?


Jens Mönig

Offline

 

#2 2007-07-17 05:18:17

Dave911
Scratcher
Registered: 2007-06-27
Posts: 36

Re: How random is random?

I have seen this in other computer generated random engines and I think I know what the problem is. Inherantly a computer cannot make something random as all its components are based on fixed rules. Therefore when a programme operates a 'random function' it actually is basing it on a variable hidden inside the programming that somehow changes overtime but not in a random fashion. In this case I think, from what you are saying about it being similiar whenever you open scratch, that this variable may be to do with how long scratch has been open and this variable resets whenever the Scratch programme is opened so really it isn't random at all. I hope this has helped.

Offline

 

#3 2007-07-17 05:30:34

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: How random is random?

Thanks, Dave. That's how it seems indeed (the randomizer being seeded by the runtime of Scratch).


Jens Mönig

Offline

 

#4 2007-07-17 15:03:35

DrJim
Scratcher
Registered: 2007-05-26
Posts: 100+

Re: How random is random?

As noted in the earlier post, the usual "random" number generator is actually a pseudo-random number generator (PRNG) and starts with a seed number (often some system variable, the choice of which is probably the reason for different behavior for the local and web programs) to generate a sequence. 

The same seed, at least for the simpler algorithms, will always generate the same sequence.  (This is a great (!) benefit for application testing.)  Having the ability to manually enter your own seed would be a nice "feature" for Jan's program - you could reproduce a sequence you liked with one pass.

If you look up pseudorandom (no hyphen) numbers in Wiki, you will see an immense amount of information and many options to produce more randomness. If you want to generate your own sequence, any of the very simple algorithms shold work fine unless you are doing encryption or Monte Carlo simulations.  For more randomness, you could add or multiply your number and Scratch's number together and truncate as needed.  (Of course, if you want to use the "Blum Blum Sbub" or the "Mersenne Twister" - go right ahead.  smile )

(As an aside, trying two or three different PRNG algorithms and evaluating the results makes a good Middle School/High School level science fair project. I've seen two or three variations on this theme and the reaction from spectators (and in some cases, judges) was much like Jens' - "You mean the numbers from my computer aren't really random?")

Offline

 

#5 2007-07-18 06:34:07

MartinWollenweber
Scratcher
Registered: 2007-04-10
Posts: 100+

Re: How random is random?

The class Random in Squeak, that is surely used for the Random-Implementation of Scratch, has following initialize-method, where the above mentioned "seed" is set. Even if you don't know Smalltalk, you can see that it uses the Timer for the seed. In the "nextValue" method of the class Random, you cann see how the "pseudo-random" is calculated.

Random>>initialize
    " Set a reasonable Park-Miller starting seed "
    [seed := (Time millisecondClockValue bitAnd: 16r3FFFFFFF) bitXor: self hash.
    seed = 0] whileTrue: ["Try again if ever get a seed = 0"].

    a := 16r000041A7 asFloat.    " magic constant =      16807 "
    m := 16r7FFFFFFF asFloat.    " magic constant = 2147483647 "
    q := (m quo: a) asFloat.
    r  := (m \\ a) asFloat.

Random>>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 answer |
    hi := (seed quo: q) asFloat.
    lo := seed - (hi * q).
    "= seed rem: q"
    aLoRHi := a * lo - (r * hi).
    answer := aLoRHi > 0.0
                ifTrue: [aLoRHi]
                ifFalse: [aLoRHi + m].
    ^ answer

Jens: I'm happy you found a way to "look under the hood" of Scratch
( see: http://scratch.mit.edu/projects/Jens/22355 )
and your way to present it in a Multimedia Scratch Project is ingeniously!

Last edited by MartinWollenweber (2007-07-18 07:04:05)


Martin
...mitmachen beim  DACH-Scratch-Wiki und Scratch-Wiki-Autor werden!

Offline

 

#6 2007-10-20 17:18:06

OJY321
Scratcher
Registered: 2007-08-03
Posts: 68

Re: How random is random?

So in english, no thanks to MartinWollenweber, it works like a claw grabber machine, over time it changes in value.

PS This random thing did make me think, how could a computer be random?

Offline

 

#7 2007-10-21 10:13:51

johnm
Scratcher
Registered: 2007-03-08
Posts: 100+

Re: How random is random?

Hi, Jens, thanks for noticing this problem and thanks to all of you who have looked into it. The problem is that Scratch sets the pseudo-random generator to the same initial value (seed) whenever Scratch starts up. Thus, the sequence of "random" numbers is exactly the same every time Scratch is restarted.

This will be fixed in the upcoming Scratch release.

OJY321, your questions shows that you really understand the idea of "randomness". How can a computer, following predictable rules, do anything unpredictable? And the answer is, it can't. But you can write programs that generate a sequence of numbers that "look" random. In fact, such "pseudo-random number generator" programs eventually repeat themselves. The one used by Scratch repeats after 4,294,967,296 numbers have been generated. (That's two raised to the 32.) The problem that Jens noticed is that Scratch is starting at the same place in this sequence every time Scratch is started.

You can get true random numbers from physical processes such as thermal noise or radioactive decay. For example, see http://www.fourmilab.ch/hotbits/. You can also flip a coin or roll dice, for that matter, but that's more work.

Random (and pseudo-random) numbers play a big role in encryption (i.e. secret codes). Kevin Karplus used randomness in his "Simon" game to "encrypt" the note sequence so the user can't figure it out (the sequence is recorded by drawing it on the screen).

  -- John

Last edited by johnm (2007-10-21 10:18:24)

Offline

 

#8 2007-12-28 12:45:03

rameninabowl
Scratcher
Registered: 2007-12-26
Posts: 100+

Re: How random is random?

Dave911 wrote:

I have seen this in other computer generated random engines and I think I know what the problem is. Inherantly a computer cannot make something random as all its components are based on fixed rules. Therefore when a programme operates a 'random function' it actually is basing it on a variable hidden inside the programming that somehow changes overtime but not in a random fashion. In this case I think, from what you are saying about it being similiar whenever you open scratch, that this variable may be to do with how long scratch has been open and this variable resets whenever the Scratch programme is opened so really it isn't random at all. I hope this has helped.

it cood also be like if u hav 3 choses,there isn't much of anything to randomly pick from!!!


http://www.rameninabowl.dragonadopters.com/dragonimage_189453_143004_pixelNA.gifhttp://rameninabowl.dragonadopters.com/dragonanimated_272756.gif

Offline

 

#9 2007-12-30 19:46:13

NobodyKnowsNorune
Scratcher
Registered: 2007-09-17
Posts: 93

Re: How random is random?

How they work is a person programmes the computer to pick numbres  based on  a varible that changes over time like Dave said. But every time it start up it has the same varaibles so the numbres always come out the same


My name is Riley                                        I ride a unicycle

Offline

 

#10 2007-12-30 19:50:08

NobodyKnowsNorune
Scratcher
Registered: 2007-09-17
Posts: 93

Re: How random is random?

Truely it is not  random at all.

I think in a random 1 to 10 numbres the 4th to 7th numbres  are all 7.
So really how random it is, is how the people made scratch whitch is not random at all.


How do you spell whitch I think that is the wrong way?


My name is Riley                                        I ride a unicycle

Offline

 

#11 2007-12-30 20:01:56

N-Wear
Scratcher
Registered: 2007-08-13
Posts: 100+

Re: How random is random?

Ya, when I made 3 sprites that were supposed to show in different spots, they where all clumped together.


If bread crumbs are better than nothing. And nothing is better than cheese cake. Then, bread crumbs are better than cheese cake!  smile
The following sentence is true. The previous sentence is false.  hmm                              Treat others the way you want to be treated!  big_smile

Offline

 

#12 2007-12-30 21:00:28

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: How random is random?

In v1.2.1, I think that the random number generator does start differently each time, but I haven't checked to be sure.  In v1.0 I believe it was always seeded the with the same seed, so that each run was the same.

I've not done test son their pseudorandom number generator, but if they used any of the standard library ones, it requires some fairly sophisticated tests to detect that it is not random.

Scratch team, care to tell us which pseudorandom number generator you used?

Offline

 

#13 2011-11-27 02:07:32

nightmarescratcher
Scratcher
Registered: 2011-10-10
Posts: 1000+

Re: How random is random?

[offtopic]I just typed in a random number in the address bar, and this came up. ;_;[/offtopic]


http://fc07.deviantart.net/fs70/f/2011/171/f/3/derpy_hooves_derp_sig_by_alicehumansacrifice1-d3jg613.png

Offline

 

Board footer