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!

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:

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.

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

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.

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.

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.

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.

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.

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 cell phone trackingLast edited by McKein on Mon Apr 09, 2012 11:54 pm; edited 1 time in total

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:

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

Please support our site. If you click through and buy from our affiliate partners, we earn a small commission.