Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: One-shot random



On 19/09/2005, at 8:32 PM, Alfred wrote:

I would like to have a composition that chooses a random number ONLY ONCE, when it's first started.

Using the "Random" object results in a new random being generated every time a frame is drawn. How can I make sure the first number that is chosen is "frozen"?

Thanks,
-Michael.



You could use a JavaScript with one output and two inputs that looked something like this:

if (outputs[0]==undefined){
    outputs[0]=math.random*(inputs[1]-inputs[0])+inputs[0]
}

This will set the random output to a number between the inputs on the first run and nothing after that.

Alfred


This won't work on screen savers. Math.random() has a nasty habit of returning the same result every time (when launching a screen saver from hot-corners or by waiting, not when clicking "Test" in the Screen Saver control panel). In fact the code above won't work at all because there is a small syntax error in it.

One solution is to manipulate Math.random() using some component of Date() to get some other result. Maybe looping over Math.random() X times, where X is the value of milliseconds in the date it the start of your composition (this was suggested by Dave Hayden to me a while ago). This is fine if you just want a seed for an integer between 0 and maybe 100, but doesn't cut it if you need some *real* randomness in your screen-saver. Reason being that any N iterations of Math.random() will always have the same result when run as a screen- saver (my guess is that it isn't being seeded correctly in that mode). It also causes a slow-down on initialisation of the composition to do anywhere up to 1000 calls to Math.random(). But here's the code anyway...

if (!outputs[0]) {
    var myD = new Date();
    var myM = myD.getMilliseconds();
    var myR;

    for (i = 0; i < myM; i++) {
        myR = Math.random();
    }

    outputs[0] = myR;
}

Another alternative is to use this randomiser macro that I adapted from existing code utilising the Mersenne Twister random number generator algorithm. It's quite a bit more random than the standard javascript random. Plus it can be called multiple times after one initialisation, possibly making it faster to use for multiple random calls. Be aware that this has some copyright usage restrictions from the authors of the original algorithm - details in the javascript code comments. Hopefully it isn't too big for the list.

Attachment: QuartzTwister.qtz
Description: application/quartzcomposer


Sam

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartzcomposer-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartzcomposer-dev/email@hidden

This email sent to email@hidden

References: 
 >One-shot random (From: Michael Kertesz <email@hidden>)
 >Re: One-shot random (From: Alfred <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.