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 
go to the radio page Live at electro-music.com radio 1 Please visit the chat
poster
 Forum index » DIY Hardware and Software » ChucK programming language
Random numbers in Windows 7
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [14 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
alexbarbed



Joined: Apr 09, 2011
Posts: 14
Location: UK

PostPosted: Mon May 09, 2011 4:43 pm    Post subject: Random numbers in Windows 7 Reply with quote  Mark this post and the followings unread

Hi all

I've been battling with randomness, running chuck on Windows 7. I have seen previous posts on the issue, but I'm not a good enough programmer to understand them... however, I have discovered this... (perhaps one of you will enlighten me!)

If I set the timing of the song to sync - e.g

beatTime - (now % beatTime) => now;

then my random numbers come out exactly the same every time. If, however, I don't use the code above to sync 'now' and just let the sound start whenever, my random numbers are perfectly usably random.

Does this mean that Windows7 uses the absolute system time to seed its random numbers? Any and all help much appreciated - thanks!

Alex
Back to top
View user's profile Send private message
skwrKing



Joined: Jul 24, 2007
Posts: 16
Location: MI

PostPosted: Wed May 11, 2011 8:11 pm    Post subject: Reply with quote  Mark this post and the followings unread

I was taught that in Windows C++, the rand function does indeed use the system clock as a seed if no other seed is provided. This site seems to verify that: "By default, the parameterless constructor of the Random class uses the system clock to generate its seed value".

I'm looking at the chuck source code right now (ulib_std.cpp), and there appears to be a function you can use to seed the random number generator yourself called "srand". It takes one int parameter as a seed value. It's not mentioned in the online docs, though.

Both my C++ and ChucK skills are very rusty, but I think what you're looking for is something like this:

2 => Std.srand;
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 371
G2 patch files: 100

PostPosted: Thu May 12, 2011 3:19 am    Post subject: Reply with quote  Mark this post and the followings unread

I would like to see a bigger code example from alexbarbd... It's pretty common practice to use the system clock to seed a random generator unless you provide a seed yourself, but I don't see how this should cause random numbers to repeat.

Unless you actually want repeating numbers, you usually seed the random generator once at the start of the program (using your own seed if you want the sequence to be consistent, otherwise allowing the system to make some seed which will usually be different each time, like the current time), and then just have it produce new numbers.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
alexbarbed



Joined: Apr 09, 2011
Posts: 14
Location: UK

PostPosted: Thu May 12, 2011 4:30 pm    Post subject: Reply with quote  Mark this post and the followings unread

Thanks very much to both of you for replying.

I've got myself into such a mess with this that I don't know what's going on. I basically had a program that was brilliantly composing completely randomly, and then I tidied it up a bit and it's stopped working randomly, even though it's full of Std.rand2 calls throughout. I get the same duff song, over and over again. It's not in a fit state to post (it's about 1000 lines long), and I'm now so sick of it I can't bear to look at it... once I've had some sleep I'll try to replicate my results in a test file and post it.

The wonders of ChucK are being offset by the drawbacks of my being a very undisciplined programmer... while it was all working I just kept hacking away, but now it isn't I'm finding it hard to retrace my steps... Embarassed

If you happened to have a self contained piece of code that created a completely random number from a seed, I'd love to see it.

I'm going to bed!
Back to top
View user's profile Send private message
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 371
G2 patch files: 100

PostPosted: Thu May 12, 2011 11:19 pm    Post subject: Reply with quote  Mark this post and the followings unread

Oh... I should have read the docs before acting like I actually know stuff, there isn't a seed function, right:

http://chuck.cs.princeton.edu/doc/program/stdlib.html

When I try out the example at the top of that page (the self-contained example you were looking for?) I get a very similar first number every time, though it's steadily increasing. This indicates that the system clock is used modulo some number. This makes me curious as to what your beatTime value in your example is. If this somehow resonates with the modulo value used when seeding from the system clock, you could get the behaviour you're seeing.

A couple of things to try:

start the program with a call to Std.rand*(), without waiting for the clock in any way, then proceed with the rest of the program. Does that make things more random?

See if you run your beatTime - (now % beatTime) => now; statement before the first ever call to Std.rand*(). If that is the case, can you switch these around, so you call rand*() before waiting on the clock? Did that solve anything?

It seems the random generator in ChucK is very crude, both from lacking a seed function and from having a semi-predictable first result from the rand2f() function.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
Antimon



Joined: Jan 18, 2005
Posts: 4145
Location: Sweden
Audio files: 371
G2 patch files: 100

PostPosted: Thu May 12, 2011 11:22 pm    Post subject: Reply with quote  Mark this post and the followings unread

Another thing you might try would be to change beatTime slighty. E.g. if your beatTime is now 2::second try to change it to 2.001:second, and see if that changes anything.

/Stefan

_________________
Antimon's Window
@soundcloud @Flattr home - you can't explain music
Back to top
View user's profile Send private message Visit poster's website
alexbarbed



Joined: Apr 09, 2011
Posts: 14
Location: UK

PostPosted: Fri May 13, 2011 5:05 am    Post subject: Reply with quote  Mark this post and the followings unread

Hi!

I'm at work now, but will give it a go tonight. I can verify that srand() doesn't work, at least not in any of the ways I've tried it. It's reassuring to know it's not just me...

I'll get back to you when I've tried it

Alex
Back to top
View user's profile Send private message
skwrKing



Joined: Jul 24, 2007
Posts: 16
Location: MI

PostPosted: Fri May 13, 2011 7:43 pm    Post subject: Reply with quote  Mark this post and the followings unread

I went about some testing tonight, and rand is doing some very strange things. I'm using the windows version of chuck-1.2.1.3

Code:

for( 0 => int i; i < 10; i++ )
{
  <<< Std.rand2f(30.0, 1000.0) >>>;
  1::second => now;
}


I ran this three times and got:

Code:

51.669362 :(float)
31.213721 :(float)
576.677755 :(float)
217.505112 :(float)
814.478286 :(float)
597.459029 :(float)
495.476852 :(float)
369.782708 :(float)
899.083529 :(float)
828.154851 :(float)


Code:

52.942289 :(float)
31.213721 :(float)
576.677755 :(float)
217.505112 :(float)
814.478286 :(float)
597.459029 :(float)
495.476852 :(float)
369.782708 :(float)
899.083529 :(float)
828.154851 :(float)


Code:

54.096805 :(float)
31.213721 :(float)
576.677755 :(float)
217.505112 :(float)
814.478286 :(float)
597.459029 :(float)
495.476852 :(float)
369.782708 :(float)
899.083529 :(float)
828.154851 :(float)


I then used a random seed in this fashion:

Code:

for( 0 => int i; i < 10; i++ )
{
  Std.srand(17);
  <<< Std.rand2f(30.0, 1000.0) >>>;
  1::second => now;
}

32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)
32.782678 :(float)


Changed the seed:

Code:

for( 0 => int i; i < 10; i++ )
{
  Std.srand(45);
  <<< Std.rand2f(30.0, 1000.0) >>>;
  1::second => now;
}

35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)
35.476547 :(float)


Code:

for( 0 => int i; i < 10; i++ )
{
  Std.srand(46);
  <<< Std.rand2f(30.0, 1000.0) >>>;
  1::second => now;
}

35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)
35.565355 :(float)


In the first set of tests I would expect the results to be wildly different each time it is ran. In the second set, all ten values are the same as you'd expect because the seed stays constant. However I would not expect incrementing the seed by one would have so minor a change on the output. So yeah it's not just you.
Back to top
View user's profile Send private message
alexbarbed



Joined: Apr 09, 2011
Posts: 14
Location: UK

PostPosted: Fri May 13, 2011 8:11 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi

Neither of them worked I'm afraid. I've got a solution now, though. I've sporked a sample which plays into a blackhole on a loop, and I poll it for its position every 10ms and then use that value whenever I need a random number. It's working pretty well so far, but then again it's ten past four in the morning and I might just be hearing things that aren't there.

Thanks for you help, and I'll let you know how it works out.
Back to top
View user's profile Send private message
alexbarbed



Joined: Apr 09, 2011
Posts: 14
Location: UK

PostPosted: Fri May 13, 2011 8:14 pm    Post subject: Reply with quote  Mark this post and the followings unread

Oh! I posted my response before I saw yours.

Yup, those results are exactly what I would expect. It would be really interesting for someone to do the same test on linux, just to see what happens...
Back to top
View user's profile Send private message
alexbarbed



Joined: Apr 09, 2011
Posts: 14
Location: UK

PostPosted: Thu May 19, 2011 1:12 pm    Post subject: Reply with quote  Mark this post and the followings unread

Hi!

I finally got it working. I started off sporking a sample and pulling the numbers out of that for randoms, but of course Chuck ran a lot faster than 44.1kHz so the whole endeavour ended up slowing down the program ludicously. Anyway, I finally understood an oblique reference from Kassen in another post about using the adc to generate random numbers and took his advice. The result works a treat. Thanks to Kassen, then.

Code:

adc => Gain g => blackhole;

Hid hi;                                       // human interface device (keyboard)
HidMsg msg;                                    // message container
0 => int device;                              // which keyboard
if( me.args() ) me.arg(0) => Std.atoi => device;
if( !hi.openKeyboard( device ) ) me.exit();            // open keyboard or quit



fun int randySubTen(int int1, int int2) {
   float sampleCapture;
   g.last() * 100000000 => sampleCapture;
   sampleCapture $ int => int intRand;
   while (! (intRand >= int1 && intRand <= int2)) {
      1::ms => now;
      g.last() * 100000000 => sampleCapture;
      sampleCapture $ int => intRand;
      if (intRand < 0) {-1 *=> intRand;}
      while ((intRand / 10) > 0) {
         if (intRand) {10 /=> intRand;}
      }
   }
   return intRand;
}

fun void qwerty() {                              //produce random number on keypress (g)
   while (10::ms => now){
      while( hi.recv( msg )){
      if( msg.isButtonDown() ) {
         if (msg.which == 34) { //g key
            <<<"you pressed the g-key and your random number is " + randySubTen(0,9) >>>;
            }
         }
      }
   }
}
            
spork ~ qwerty();

while (true) {    1000::ms => now;}


Maybe some will find this useful too.

Alex
Back to top
View user's profile Send private message
McKein



Joined: Feb 22, 2012
Posts: 3
Location: Norway

PostPosted: Wed Feb 22, 2012 6:27 am    Post subject: Reply with quote  Mark this post and the followings unread

Thanks, Alex, i was going to ask the same question before i saw your response. Hope to have no troubles and understand an oblique reference from Kassen too Laughing cell phone tracking
Last edited by McKein on Mon Apr 09, 2012 11:54 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
heyandy889



Joined: Mar 02, 2012
Posts: 2
Location: Location

PostPosted: Fri Mar 02, 2012 11:14 pm    Post subject: Reply with quote  Mark this post and the followings unread

(bit of a thread necro . . . )

Yes! Yes! I was wondering why in the world I was getting the same melody every time. Beautiful! I tried the code from alexbarbed (or Kassen?) and it works great. Thanks a lot.

One small problem I ran into with this new code - the first two notes (chosen by random numbers) would be the same each time. Then, the notes would deviate away into randomness as desired. Anyways, my short fix is this:

Code:
adc => Gain g => blackhole;    //our leet hardware random number generator

<<< "Welcome" >>>;
1::second => now;              //wait for adc to "fire up."
//                               only need like 250 ms,
//                               but 1 second is nicer for the user to look at.

//some short intro code

while(true){
    //use randySubTen() to strategically choose a note
}


Thanks again. I thought I was bonkers when the random number generator kept popping out the same melody.
Back to top
View user's profile Send private message
JonSand



Joined: Oct 10, 2012
Posts: 1
Location: Miami

PostPosted: Wed Oct 10, 2012 6:58 am    Post subject: Reply with quote  Mark this post and the followings unread

Antimon wrote:
Another thing you might try would be to change beatTime slighty. E.g. dieta dukana if your beatTime is now 2::second try to change it to 2.001:second, and see if that changes anything.

/Stefan


Hey guys

I'm new on this forum and wanted to start of with saying thanks. This thread helped to solve my problem.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [14 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