This question already has an answer here:
- Pass Variables by Reference in Javascript 10 answers
How do you save the output of a random number generator function to be recalled in a different function?
For example, in the code below, I would be using the function randNum() to roll the dice and return the value of w to the saveDiceNumber() function as a variable.
Then I would save the first value received from the randNum() function into the new variable x located in the saveDiceNumber() function. Then I would like to repeat the process for variables y and z, without losing the newly created value of x.
So in essence, I would like to create a single function that uses the same random number generator to create 3 different variables and then recall the value of those variables without having to re-generate/re-roll random numbers.
The random number generator function:
function randNum(){
var w = Math.floor(Math.random()*Math.floor(6));
return w;
}
Function where RNG output should be saved:
function saveDiceNumber(){
var w = randNum(); //roll the dice
var x = w; //first output of w
var y = w; //second output of w
var z = w; //third output of w
}
Aucun commentaire:
Enregistrer un commentaire