[Newbies] Randomness

Levente Uzonyi leves at elte.hu
Tue Apr 5 00:32:47 UTC 2011


On Mon, 4 Apr 2011, michael rice wrote:

> I forgot to mention, I'm using the random number tile and scripting. Does that make it more difficult?

Not really, RandomNumberTile uses the global random number generator of 
Collection. I don't know how/why is it serialized/restored, but here's a 
method which should help:

RandomNumberTile >> #comeFullyUpOnReload: smartRefStream

 	| seed |
 	[
 		seed := (Time millisecondClockValue bitAnd: 16r3FFFFFFF) bitXor: self hash.
 		seed := seed bitXor: (Time totalSeconds bitAnd: 16r3FFFFFFF).
 		seed := seed bitXor: 16r3FFFFFFF atRandom.
 		seed = 0 ] whileTrue. "Try again if ever get a seed = 0"
 	Collection mutexForPicking critical: [
 		Collection randomForPicking seed: seed ].
 	^super comeFullyUpOnReload: smartRefStream

It's drawback is that it will reseed the rng for every RandomNumberTile. 
The best solution would be if Etoys would use a custom Random instance or 
a subclass where this is done by default.


Levente

>
> Michael
>
> --- On Mon, 4/4/11, Levente Uzonyi <leves at elte.hu> wrote:
>
> From: Levente Uzonyi <leves at elte.hu>
> Subject: Re: [Newbies] Randomness
> To: "A friendly place to get answers to even the most basic questions about Squeak." <beginners at lists.squeakfoundation.org>
> Date: Monday, April 4, 2011, 8:08 PM
>
> On Mon, 4 Apr 2011, michael rice wrote:
>
>> I just noticed that when I load a morphic project I get the same stream of random numbers each time. Is there a simple way to install a new different random seed each time the project is loaded?
>
> If you serialize the Random object, then it will have the same state when it's restored. I guess you should create a subclass of Random, which implements #comeFullyUpOnReload:. That method can reinitialize the seed of the random number generator. You may want to check UUIDGenerator >> #makeSeed if you don't know how to get a good seed for your rng.
>
>
> Levente
>
>>
>> Michael
>>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list