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?
Offline
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
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.
)
(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
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)
Offline
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
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!!!
Offline
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
Offline
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?
Offline
Ya, when I made 3 sprites that were supposed to show in different spots, they where all clumped together.
Treat others the way you want to be treated!
Offline
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
[offtopic]I just typed in a random number in the address bar, and this came up. ;_;[/offtopic]

Offline