Help with JavaScript random variables (SL360)

I'm hoping that someone can help me out with some JavaScript. I'm trying to generate random values for my variables based upon an initial random number.

I'm using SL360 to generate a random value between 50 - 200 for v_deadLoad. I then need to generate random values for the remaining variables based on the value SL generates for v_deadLoad.

For example, v_roofliveLoad needs to be a random number that is 1/8 - 1/4 the value of v_deadLoad, and v_rainLoad needs to be a random number that is 1/10 - 1/5 the value of v_deadLoad.

I know how to do the basic assignments. I'm just not sure how randomly generate the fractional values for the ranges.

var v_deadLoad, v_roofliveLoad, v_rainLoad;

var player = GetPlayer();

v_deadLoad = player.GetVar("v_deadLoad");

v_roofliveLoad = v_deadLoad*.125;
v_rainLoad = v_deadLoad*.1;




Any help that anyone can provide will be greatly appreciated!



4 Replies
Sam Hill

Hi Chris, you can utilise a JavaScript function that will return a random number in a specific number range.

function randomiser(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);

I think this might be what you are looking for.

v_deadLoad = player.GetVar("v_deadLoad"); 

maxroof = v_deadLoad/ 4; // one quarter

minroof = v_deadLoad / 8; // one eigth

v_roofliveLoad = randomiser(minroof, maxroof);

maxrain = v_roofliveLoad / 5;

minrain = v_roofliveLoad / 10;

v_rainLoad = randomiser(minrain, maxrain);