Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Math.random returns always the same number Extendscript

  • Math.random returns always the same number Extendscript

    Posted by Miguel De mendoza on August 30, 2016 at 11:13 am

    Hi, I got this function:
    function randomIntInRange(min, max) {
    now = new Date();
    seed = now.getMilliseconds();
    return Math.floor(Math.random(seed) * (max - min + 1) + min);
    }

    I used it for a long time with no problems(on mac and windows). But since yesterday, is behaving in a strange way. I use it on a loop, and is returning me always the same number when I run the script. I tryed to use the red dot on the Toolkit to see what happens, clickiing on the play button every loop, but then, it works properly and returns diferent numbers.
    Anybody can figure out what is happening?

    PD: The same script with no changes worked the past days.

    Miguel De mendoza replied 9 years, 8 months ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    August 30, 2016 at 12:06 pm

    It’s a known synchronization issue that was introduced in CC 2014 (I think). Adobe’s solution was to create a new generateRandomNumber() function. If there’s any chance your script could be used in an older version of AE, you’ll need to test for version number before using it. This is how I do it:


    function getRandom(){
    return parseFloat(app.version) > 13.6 ? generateRandomNumber() : Math.random();
    }

    Then just use getRandom() wherever you would use Math.random().

    BTW, as far as I know, Math.random() doesn’t take any parameters, so I don’t think your seed code makes any difference.

    Dan

  • Miguel De mendoza

    August 30, 2016 at 12:17 pm

    That was awesome!
    Thanks Dan!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy