[chuck-users] Kassen's Phasor

Michael Heuer heuermh at gmail.com
Thu Jul 16 00:40:32 EDT 2009


Kassen wrote:

> ...What we also can't have is random melodies to play sounds that are using
> this technique, at least not unless we implement our own random number
> generator as a public class. Sadly we can't have our cake and put it in the
> blender, at least not at the same time.

Sure we can.

It's not always terribly easy to implement a pseudo-random number
generator, but it is straightforward, and often they are very well
documented.  Consider the very simple Xorshift RNGs:

// xorshift random number generators
// see
// Marsaglia, G. "Xorshift RNGs", Journal of Statistical Software,
Vol. 8, Issue 14, Jul 2003.
// http://www.jstatsoft.org/v08/i14

class rng
{
    int seed;
    2147483647.0 => float MAX_VALUE;

    fun int nextInt()
    {
        return 0;
    }

    fun float nextFloat()
    {
        return 0.0;
    }

    fun float nextFloat(float n)
    {
        return 0.0;
    }
}

class xorshift extends rng
{
    fun int nextInt()
    {
        return seed;
    }

    fun float nextFloat()
    {
        return (nextInt()/MAX_VALUE);
    }

    fun float nextFloat(float n)
    {
        return (n * nextFloat());
    }
}

class xorshift13_17_5 extends xorshift
{
    fun int nextInt()
    {
        (seed << 13) ^=> seed;
        (seed >> 17) ^=> seed;
        (seed << 5) ^=> seed;
        return seed;
    }
}

xorshift13_17_5 xor;
2463534242 => xor.seed;

// not sure if these are working correctly, since chuck uses an unsigned integer

while(true)
{
        1::second => now;
        //<<<xor.nextInt()>>>;
        //<<<xor.nextFloat()>>>;
        <<<xor.nextFloat(100.0)>>>;
}

The paper linked in the comments above gives 80 different combinations
for bit shifting part above:

 1, 3,10
 1, 5,16
 1, 5,19
 1, 9,29
 1,11, 6
 1,11,16
 1,19, 3
 1,21,20
 1,27,27
 2, 5,15
 2, 5,21
 2, 7, 7
 2, 7, 9
 2, 7,25
 2, 9,15
 2,15,17
 2,15,25
 2,21, 9
 3, 1,14
 3, 3,26
 3, 3,28
 3, 3,29
 3, 5,20
 3, 5,22
 3, 5,25
 3, 7,29
 3,13, 7
 3,23,25
 3,25,24
 3,27,11
 4, 3,17
 4, 3,27
 4, 5,15
 5, 3,21
 5, 7,22
 5, 9,7
 5, 9,28
 5, 9,31
 5,13, 6
 5,15,17
 5,17,13
 5,21,12
 5,27, 8
 5,27,21
 5,27,25
 5,27,28
 6, 1,11
 6, 3,17
 6,17, 9
 6,21, 7
 6,21,13
 7, 1, 9
 7, 1,18
 7, 1,25
 7,13,25
 7,17,21
 7,25,12
 7,25,20
 8, 7,23
 8,9,23
 9, 5,1
 9, 5,25
 9,11,19
 9,21,16
10, 9,21
10, 9,25
11, 7,12
11, 7,16
11,17,13
11,21,13
12, 9,23
13, 3,17
13, 3,27
13, 5,19
13,17,15
14, 1,15
14,13,15
15, 1,29
17,15,20
17,15,23
17,15,26

   michael


More information about the chuck-users mailing list