[chuck-users] random seed

Stefan Blixt stefan.blixt at gmail.com
Sat Sep 11 12:21:21 EDT 2010


Gotcha! Or at least, I can see a flaw here in ChucK's randomizers: there is
only one of them! My immediate reaction to seeing your code was that it
ought to be simplified to this:

while (1)
{
    Std.srand(1200); //set your seed
    for(0 => int i; i < 8; i++)
    {
        <<<Std.rand2( 1 , 1000 )>>>;
    }
    <<<"--------------", "we always get the same numbers" >>>;
    2::second=>now;

}

But this won't work if you use the randomizer elsewhere in your code. if
someone calls Std.rand2() before the loop above has finished, the random
sequence will be corrupted. It will work here though, since there is no wait
statement inside the for loop, but I suspect Lars's code is more complicated
than this.

Other programming languages will usually have some kind of randomizer object
(e.g. "Random" in Java or the result of the seed function in PovRAY), that
you instantiate with a seed (if you want) and then use independent from
other random instances. If we had that you could write this code:

0 => int resetSeed;
Std.makeRandomizer() => Random r;
while (1)
{
if (resetSeed) {
r.srand(1200); //set your seed
}
    for(0 => int i; i < 8; i++)
    {
        <<<r.rand2( 1 , 1000 )>>>;
    }
    <<<"--------------", "we always get the same numbers" >>>;
    2::second=>now;

}

...without clashing with other usage of randomizers (I made up some function
and class names).

Was it something like this that you ran into, Lars?

/Stefan
2010/9/11 Kassen <signal.automatique at gmail.com>

> Hey, Lars!
>
> I'm not Stephan, but I'm still a bit confused as well so I thought I'd
> answer anyway.
>
> Std.srand(1200); //set your seed
>>
>
> Looks fine
>
>     <<<"--------------", "we always get the same numbers" >>>;
>>
>
> That's what we'd expect, yes.
>
>
>> Hope this helps!
>>
>>  Well.... no, not really.
>
> What happens if you don't set the seed with srand(), do you still get the
> same numbers? As I understand things now you should get exactly what you are
> looking for by simply not setting the seed in line 1. Could you verify that
> by not setting the seed at all you get different numbers each time you run
> your program? If not we have a bug. I seem to remember there was a issue
> like that on Windows, which might be caused by Windows (last time I looked)
> not having the "storage" of OS-event based random numbers that Unix
> typically uses; that might be the cause. That's no excuse though; as we are
> not trying to secure nuclear missiles (I hope!) something like the time in
> ms should yield a perfectly serviceable seed at VM startup.
>
> Yours,
> Kas.
>
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
>


-- 
Release me, insect, or I will destroy the Cosmos!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20100911/c40da8d4/attachment.htm>


More information about the chuck-users mailing list