electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
Random is consistent
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [13 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
steveJ0



Joined: Oct 28, 2006
Posts: 5
Location: University Park

PostPosted: Tue Feb 27, 2007 10:44 pm    Post subject: Random is consistent
Subject description: i get the same random values every time
Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
steveJ0



Joined: Oct 28, 2006
Posts: 5
Location: University Park

PostPosted: Tue Feb 27, 2007 10:58 pm    Post subject: Reply with quote  Mark this post and the followings unread

ok, i just checked and i do get random values when running Linux. the "randomness" only selects the same values in windows... Also it seems the code got a little messed up, so I've attached the original .ck file. I'd still like to know why this file does the same thing when run in windows. Thanks for your time.


response4.ck
 Description:

Download
 Filename:  response4.ck
 Filesize:  2.68 KB
 Downloaded:  332 Time(s)

Back to top
View user's profile Send private message
moudi



Joined: Oct 07, 2006
Posts: 63
Location: Bern Switzerland

PostPosted: Wed Feb 28, 2007 1:32 am    Post subject: Re: Random is consistent
Subject description: i get the same random values every time
Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
ge



Joined: Aug 13, 2006
Posts: 108
Location: Palo Alto, CA

PostPosted: Thu Mar 01, 2007 12:43 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Sun Mar 04, 2007 5:46 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Tue Mar 20, 2007 7:37 am    Post subject: Reply with quote  Mark this post and the followings unread

This just raises one question...

How can you have a random seed if random itself is deterministic?

Thanks

Rhys
Back to top
View user's profile Send private message
kijjaz



Joined: Sep 20, 2004
Posts: 765
Location: bangkok, thailand
Audio files: 4

PostPosted: Tue Mar 20, 2007 12:58 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Wed Mar 21, 2007 2:10 am    Post subject: Reply with quote  Mark this post and the followings unread

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 Smile.

If you need more on randomness and computer music, look up that article by Ffitch; it's good.

_________________
Kassen
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dr. Spankenstein



Joined: Mar 03, 2007
Posts: 136
Location: Cambridge
Audio files: 1

PostPosted: Wed Mar 21, 2007 9:52 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
moudi



Joined: Oct 07, 2006
Posts: 63
Location: Bern Switzerland

PostPosted: Wed Mar 21, 2007 11:14 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Visit poster's website
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Wed Mar 21, 2007 12:17 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
kijjaz



Joined: Sep 20, 2004
Posts: 765
Location: bangkok, thailand
Audio files: 4

PostPosted: Thu Mar 22, 2007 1:17 am    Post subject: Reply with quote  Mark this post and the followings unread

Code:
Gain g; <<<g>>>;


I guess the address of this new Ugen quite varies
but can i cast to Int?
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger
Kassen
Janitor
Janitor


Joined: Jul 06, 2004
Posts: 7678
Location: The Hague, NL
G2 patch files: 3

PostPosted: Thu Mar 22, 2007 4:12 am    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [13 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use