Author |
Message |
steveJ0
Joined: Oct 28, 2006 Posts: 5 Location: University Park
|
Posted: Tue Feb 27, 2007 10:44 pm Post subject:
Random is consistent Subject description: i get the same random values every time |
 |
|
Hey, maybe you can help me with this. I think I just might be using the wrong random function, but the code where this problem becomes most annoying is listed below. Let me know what you think. Run it a few times and listen to the "motiv" array (which should be randomly created each time the file is run). Maybe I'm the only one having the problem. Thanks!
Code: |
//Ugen
Mandolin t => Gain g => Envelope l => Echo e => Pan2 p => dac;
Rhodey t2 => Gain g2 => Envelope l2 => Echo e2 => Pan2 p2 => dac;
//Panning
1.0 => float osc1pan => p.pan;
-1.0 => float osc2pan => p2.pan;
//Effects
4.4::second => e.max => e2.max;
20::ms => e.delay => e2.delay;
.5 => dac.gain;
10::ms => l.duration => l2.duration;
//Timing
.5:: second => dur beat;
beat - ( now % beat ) => now;
//Array
[ 55, 57, 59, 60, 62, 64, 66, 67] @=> int scale[];
int motiv[8];
8 => int count;
0 => int index;
.083 => float panner;
while( true )
{
8 => count;
do
{
<<< "first loop" >>>;
0 => index;
t.noteOn(1.0);
l.keyOn();
if( count == 8 )
{
scale[ Math.rand2( 0, 7 ) ] @=> motiv[0];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[1];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[2];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[3];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[4];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[5];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[6];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[7];
0 => count;
}
Std.mtof( motiv[ count ] ) => t.freq;
.5::beat => now;
panner -=> osc1pan;
osc1pan => p.pan;
if( count == 7 )
{
t2.noteOn(1.0);
l.keyOff();
l2.keyOn();
while( index <3> t2.freq;
1::beat => now;
index++;
( 2 * panner ) +=> osc2pan;
osc2pan => p2.pan;
}
0 => count;
l2.keyOff();
}
else
{ count++; }
}until( osc1pan <0> count;
do
{
<<< "second loop" >>>;
0 => index;
t.noteOn(1.0);
l.keyOn();
if( count == 8 )
{
scale[ Math.rand2( 0, 7 ) ] @=> motiv[0];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[1];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[2];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[3];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[4];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[5];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[6];
scale[ Math.rand2( 0, 7 ) ] @=> motiv[7];
0 => count;
}
Std.mtof( motiv[ count ] ) => t.freq;
.5::beat => now;
panner +=> osc1pan;
osc1pan => p.pan;
if( count == 7 )
{
t2.noteOn(1.0);
l.keyOff();
l2.keyOn();
while( index <3> t2.freq;
1::beat => now;
index++;
( 2 * panner ) -=> osc2pan;
osc2pan => p2.pan;
}
0 => count;
l2.keyOff();
}
else
{ count++; }
}until( osc1pan > .98 );
}
|
Hope that's not too messy for you all.
-Steve from Penn State |
|
Back to top
|
|
 |
steveJ0
Joined: Oct 28, 2006 Posts: 5 Location: University Park
|
|
Back to top
|
|
 |
moudi
Joined: Oct 07, 2006 Posts: 63 Location: Bern Switzerland
|
Posted: Wed Feb 28, 2007 1:32 am Post subject:
Re: Random is consistent Subject description: i get the same random values every time |
 |
|
hi steveJ0
steveJ0 wrote: | scale[ Math.rand2( 0, 7 ) ] @=> motiv[0]; |
I can't find a rand2 function in the API documentation for the Math namespace.
I usually use the identical named function in the Std namespace.
how is it working when you use the one from Std?
jassas
/moudi |
|
Back to top
|
|
 |
ge

Joined: Aug 13, 2006 Posts: 108 Location: Palo Alto, CA
|
Posted: Thu Mar 01, 2007 12:43 am Post subject:
|
 |
|
The windows c runtime rand function can get pretty uh deterministic.
You can try seeding the random number generator by calling Std.srand( int ). This will cause the sequence to be completely deterministic, but you can change that by providing a different seed.
I hope this help.
Ge! |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sun Mar 04, 2007 5:46 pm Post subject:
|
 |
|
For even better randomness it might be interesting to use your ADC (unconnected or with a cheap/bad piece of gear hooked up), multiplying a single value by something large, casting to int and using this as a seed.
The worse your soundcard, the better the result (!!!!) at least some of that should be resistor noise which is a honest and true source of total quantum-level randomness (or so it goes).
If the C runtime algorithem won't do then you could roll your own as well; random number generators need not be that complicated (and several can be found online or in literature). As long as you pair it with a good way of generating seeds you should be fine. _________________ Kassen |
|
Back to top
|
|
 |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Tue Mar 20, 2007 7:37 am Post subject:
|
 |
|
This just raises one question...
How can you have a random seed if random itself is deterministic?
Thanks
Rhys |
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Tue Mar 20, 2007 12:58 pm Post subject:
|
 |
|
Dr. Spankenstein:
I studied about this pseudo-random number thing while taking a Simulation course..
but now i just don't know how to explain..
you can check out
http://en.wikipedia.org/wiki/Pseudorandom_number_generator
the random number we're creating from a random seed here is pseudo-random.
and yes, the algorithm makes it deterministic
but it can approximate the properties of random numbers (from wikipedia.org) |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Wed Mar 21, 2007 2:10 am Post subject:
|
 |
|
I also recomend the exelent article by John Ffitch on using randomness in musical code that's in the Csound handbook.
Examples he gives;
A sugested random number generator for 32 bit C was;
X(n+1) = 1103515245 X(n) +12345
where only the top 16 bits would be used.
That one's not supposed to be particularly good so Ffitch sugests this;
X(n) = ( X(n-24) + X(n-55) ) mod 2^31
Which would need a array of 55 values that would all need to be filled up front and would need functions to shift it so that might be unpractical.
Csound uses a 16 bit pseudo random generator;
Xn = (15625 X(n-1) + 1) mod2^16
(outermost brackets there are mine because otherwise it makes no sense at all, I think that might be a missprint in the Csound book (I have the first edition)
Anyway, that last one is upposedly not that random (in the mathematical sense) either but it's what Csound uses for hystorical reasons.
All of those will need a seed but they might be good enough if you want to implement your own.
There are, BTW, very good reasons to use "predictable" randomness in computer music programs because they make pieces repeatable. If you need something better I sugest using my trick of "stealing" a seed from the ADC. I think modern processors sometimes also have a build in random number generator and Linux (and likely OSX?) have facilities for randomness in the OS that should be very random indeed.
If you want to get REALY clever you could -uppon runing a ChucK program- require the user to wiggle his mouse for a while and use that to base a seed on. If you use that combined with the second algorithem above that should give you something quite random indeed .
If you need more on randomness and computer music, look up that article by Ffitch; it's good. _________________ Kassen |
|
Back to top
|
|
 |
Dr. Spankenstein
Joined: Mar 03, 2007 Posts: 136 Location: Cambridge
Audio files: 1
|
Posted: Wed Mar 21, 2007 9:52 am Post subject:
|
 |
|
Is there anyway of getting the current computer time in Chuck as the current time in seconds would be an excellent way to seed a seemingly random number? |
|
Back to top
|
|
 |
moudi
Joined: Oct 07, 2006 Posts: 63 Location: Bern Switzerland
|
Posted: Wed Mar 21, 2007 11:14 am Post subject:
|
 |
|
hi
Dr. Spankenstein wrote: | Is there anyway of getting the current computer time in Chuck as the current time in seconds would be an excellent way to seed a seemingly random number? |
you can get the actual (running)time of the virtual machine. this should be similar...
Code: | float seed;
now / samp => seed;
<<<seed>>>; |
regards
/moudi |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Wed Mar 21, 2007 12:17 pm Post subject:
|
 |
|
That's good in some cases but not so good when often starting ChucK by calling it with the .ck as the parameter instead of starting the VM, then throwing in files.
I'd like to be able to get the computer time as well, BTW. Maybe as a duration since the last midnight?
Dates would be more tricky. I think ChucK doesn't suport "year" as a native duration to get around leap-years. It doesn't support "month" either, probably for the same reason. Maybe we could use Unix time.... _________________ Kassen |
|
Back to top
|
|
 |
kijjaz

Joined: Sep 20, 2004 Posts: 765 Location: bangkok, thailand
Audio files: 4
|
Posted: Thu Mar 22, 2007 1:17 am Post subject:
|
 |
|
I guess the address of this new Ugen quite varies
but can i cast to Int? |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Thu Mar 22, 2007 4:12 am Post subject:
|
 |
|
I don't think you can.
I don't think you can do anything to it since the only way of adressing it is the reference and so any way of adressing that would be directed at the gain itself and casting a Gain to a int would be a bit silly.
What might be a option is using the brand new parameters that can be used when calling a file and using those to set a seed, just pressing randomly on the num-pad for a bit to generate it. _________________ Kassen |
|
Back to top
|
|
 |
|