Skip to main content
tomia46669570
Known Participant
July 22, 2015
Question

Using Javascript's Math.random in AE Scripting Code || Not so random?

  • July 22, 2015
  • 2 replies
  • 3410 views

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

This topic has been closed for replies.

2 replies

Dan Ebberts
Community Expert
Community Expert
July 26, 2015

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

tomia46669570
Known Participant
July 27, 2015

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

stib
Inspiring
September 5, 2015

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.

Tomas Sinkunas
Legend
July 23, 2015

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.

tomia46669570
Known Participant
July 23, 2015

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

tomia46669570
Known Participant
July 23, 2015

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