Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Random Hexadecimal digits

  • Random Hexadecimal digits

    Posted by Navarro Parker on November 28, 2012 at 10:52 pm

    I have this expression that will display random hex letters if put into a text layer’s Source Text parameter:

    rate = 5;
    seed = Math.floor(time*rate);
    seedRandom(seed,true);
    String.fromCharCode(65 + Math.floor(random(6)));

    If I lower the starting CharCode from 65 to 48 to include numbers, I get unwanted punctuation symbols from 58~64.

    How can I generate purely hex digits?

    Dan Ebberts replied 13 years, 9 months ago 2 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    November 29, 2012 at 12:09 am

    Try changing the last line to this:

    Math.floor(random(16)).toString(16).toUpperCase();

    Dan

  • Navarro Parker

    November 29, 2012 at 2:00 am

    Brilliant as always!

    Can you explain a little bit how that works? Are the symbols being ignored because of the .toUpperCase command? The (16) doesn’t seem to correspond to where the letters are in ASCII table.

  • Dan Ebberts

    November 29, 2012 at 2:13 am

    Using 16 as an argument for toString() causes the random integer (0 to 15) to be converted to a hexidecimal (base 16) representation of the number. If you leave off the toUpperCase(), “A” thru “F” will be “a” thru “f”. The ASCII table doesn’t really enter into it.

    Dan

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