Using Javascript's Math.random in AE Scripting Code || Not so random?
Copy link to clipboard
Copied
Hey Friends,
I'm practicing how to create elements really quickly via scripting.
I'm using Javascript's Math.random.
However, I'm getting the impression that it's not very random.
Here's my code.
{
app.beginUndoGroup("My Code");
var numObj=100;
var myComp = app.project.activeItem;
var mySolid = myComp.layers.addSolid([1,1,1], "my square", 1920, 1080, 1);
mySolid.moveToEnd();
var brandcolors=[[191,191,191],[242,230,219],[89,71,55],[144,199,186],[219,242,230]];
var easeIn = new KeyframeEase(0.5, 50);
var easeOut = new KeyframeEase(0.75, 85);
var Exp="";
//for (u = 0; u<numObj; u++)
//{
// var myRandom= ((1920-0)*Math.random())+0;
// alert(Math.round(myRandom));
// }
for (m = 0; m<numObj; m++)
{
var compBG = brandcolors[m%brandcolors.length]/255;// comp background color
var myX= Math.round(((1920-0)*Math.random())+0);
var myY= Math.round(((1080-0)*Math.random())+0);
//alert("I start out "+myX);
var myCirclesize=((200-20)*Math.random())+20;
var randomStrokeWidth= ((30-10)*Math.random())+10;
var myShape= myComp.layers.addShape();
myShape.moveToBeginning();
myShape.name="tomi"+m+"name";
var myPosition = myShape.property("position");
//alert("I end up "+myX);
myPosition.setValue([myX,myY]);
myPosition.expression=Exp;
//ELIPSE SIZE
var shapeGroup = myShape.property("Contents").addProperty("ADBE Vector Group");
var Ellipse = shapeGroup.property("Contents").addProperty("ADBE Vector Shape - Ellipse");
Ellipse.property("ADBE Vector Ellipse Size").setValue([myCirclesize,myCirclesize]);
//STROKE COLOR
var EllipseStroke = shapeGroup.property("Contents").addProperty("ADBE Vector Graphic - Stroke");
EllipseStroke.property("ADBE Vector Stroke Color").setValue(compBG);
//STROKE SIZE
EllipseStroke.property("ADBE Vector Stroke Width").setValue(randomStrokeWidth);
//STROKE TYPE
EllipseStroke.property("ADBE Vector Stroke Line Cap").setValue(2);
//TRIM PATHS
var randomStartTime=((1-1)*Math.random())+1;
var randomDur=((1-.6)*Math.random())+.6;
var myTrim = myShape.property("Contents").addProperty("ADBE Vector Filter - Trim");
myTrim.property("ADBE Vector Trim End").setValuesAtTimes([randomStartTime,randomStartTime+randomDur],[0,100]);
myTrim.property("ADBE Vector Trim Start").setValuesAtTimes([randomStartTime+.2,.2+randomStartTime+randomDur],[0,100]);
myTrim.property("ADBE Vector Trim Offset").setValuesAtTimes([randomStartTime,randomStartTime+randomDur],[0,90]);
//SETTING EASE ON TRIM PROPERTY
myTrim.property("ADBE Vector Trim End").setTemporalEaseAtKey(1, [easeIn], [easeOut]);
myTrim.property("ADBE Vector Trim Start").setTemporalEaseAtKey(2, [easeIn], [easeOut]);
myTrim.property("ADBE Vector Trim Offset").setTemporalEaseAtKey(1, [easeIn], [easeOut]);
myTrim.property("ADBE Vector Trim Offset").setTemporalEaseAtKey(2, [easeIn], [easeOut]);
// alert("And I'm still "+myX);
}
app.endUndoGroup();
}
When I tell it to alert me of the numbers it's generating, the values I get are more random. When I comment out my alert, I get several repeats of the same values.
Any ideas on what's going on?
Thanks in advance!
Best,
Tomi
Copy link to clipboard
Copied
Hi Tomi,
Math.random() uses the current time in JavaSript and theres no straight forward way to have a seed for that.
However, you can import script https://github.com/davidbau/seedrandom/blob/master/seedrandom.js to your code and use Math.seedrandom('foo') that will generate a random seed.
If you download that script, you can import using:
#include 'path/to/lib/seedrandom.js'
Cheers,
Tom.
Copy link to clipboard
Copied
Hey Tom,
Thanks for the link. Will try to use it now!
A quick question in the meantime....what do you mean by Math.random() uses the current time?
Thanks!
Tomi
Copy link to clipboard
Copied
Actually I think i understand..I think overtime it generated different numbers but it does the calculation so fast that in several instances, it gives me the repeat of the same "random" number
Copy link to clipboard
Copied
The reason you get similar or identical random numbers is because you use math.random() function.
It does not matter how fast the calculations are, easy time you do Math.random() you will get different results.
Cheers.
Copy link to clipboard
Copied
Just curious--are you running AE CC 2015 on a Mac? I can confirm that Math.random() appears to be messed up in that configuration, but it seems fine on earlier versions of AE or on Windows.
Dan
Copy link to clipboard
Copied
Hey Dan,
That is correct! I'm using AE CC 2015 on a Mac Pro. Glad to know someone else has encountered the same issue. Hopefully it'll be fixed in the next update.
Thank you!
Best,
Tomi
Copy link to clipboard
Copied
Did you get the seedrandom.js include to work? I got an error in the extendscript toolkit:
Cannot execute script in target engine 'main'!
(#1) Object of type Date found where a Number, Array, or Property is needed
I'm using cc 2015 on a mac too.
Copy link to clipboard
Copied
I was running into the same problem in AE CC 2015 where I had a loop that I was hoping would generate unique random numbers, rounded to integers. However, they were all outputting the same. I sloved this by using
performance.now()
and multiplying by 10,000 (if I remember right; the code isn't in front of me).
https://developers.google.com/web/updates/2012/08/When-milliseconds-are-not-enough-performance-now
Copy link to clipboard
Copied
I'm getting this error too on CC2015, Mac. To return a random number, I'm using:
Math.seedrandom();
return Math.random();
Unable to execute script at line 203. Object of type Date found where a Number, Array, or Property is needed
Copy link to clipboard
Copied
Appreciate your reply to this comment! Hadn't yet tried to reference an external document, but thankfully, Adobe's created a fix for this issue.
"
The function, generateRandomNumber(), generates random numbers. Use this function instead of Math.random() when you want to generate random numbers.
generateRandomNumber() avoids a problem where Math.random() does not return random values due to a concurrency issue with multiple CPU threads."

