# [chuck-users] easy random numbers

Michael Heuer heuermh at gmail.com
Mon Nov 19 14:14:31 EST 2007

```Pseudo-random number generator functions are easy to come by, here's
an example from "Xorshift RNGs" (Marsaglia, 2003;
http://www.jstatsoft.org/v08/i14)

2463534242 => int seed;

fun int xor(int y) {
(y << 13) ^=> y;
(y >> 17) => y;
(y << 5) ^=> y;
return y;
}

seed => int y;
while(true)
{
1::second => now;
xor(y) => y;
<<<y>>>;
}

But again, you need a seed to begin with.

michael

On Nov 19, 2007 12:52 PM, David Powers <cyborgk at gmail.com> wrote:
> I guess you need some pseudo-random number to multiply by, such as the
> system time in milliseconds... However I'm not sure how one would get
> the system time into ChucK.
>
> I suppose that you could run some sort of batch script, or say a
> couple lines of Python, that either gets the current system time, or
> creates some type of random seed number; then generates a ChucK file
> with that seed number as a global variable.
>
> ~David
>
>
> On Nov 19, 2007 12:26 PM, jakob kaiser <jakobala at hotmail.com> wrote:
> >
> >  Hi,
> >  I want to write a small script for ear training with Chuck. Therefore Chuck
> > should produce values that seem to be quite random. But it seems like that
> > the standard functions here are too deterministic. I know that a
> > non-deterministic random function would not be possible, but most
> > programming languages provide functions that create at least new numbers
> > every time I start the same code. However, in chuck something like the
> > following always prints the same numbers when I run it:
> >
> >  while(true){
> >    1::second => now;
> >    Math.rand2(50, 1000) => int rndNumber;
> >    <<<rndNumber>>>;
> > }
> >
> >  Is there an easy way to get something more unpredictable? I don't want to
> > go deep into random generator programming. But the function does not really
> > what you would expect of a random generator, does it?
> >
> >  Cheers
> >
> >  Jakob
```