0

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(){
    return Math.floor(Math.random()*6); 
}

Function where RNG output should be saved:

function saveDiceNumber(){
    var w = randNum(); //roll the dice for the first permanent value

    var x = randNum(); //second output of dice
    var y = randNum(); //third output of dice
    var z = randNum(); //forth output of dice

    pickFruit(x);
    }

Where the output of saveDiceNumber() will be used:

   function pickFruit(letter){

       switch(letter){
       case(1):
       console.log("Apples");
       break;
       case(2):
       console.log("Pears");
       break;
       case(3):
       console.log("Bananas");
       break;
       case(4):
       console.log("Mangos");
       break;
       case(5):
       console.log("Pineapples");
       break;
       case(6):
       console.log("Avacados");
       break;
}

Typing: saveDiceNumber() will console log different fruit every time, even though the function pickFruit(x) is the same.

I need the value of the variable x to permanently equal to = the value generated by one usage of the randNum() function.

So if I roll a 3, then x needs to be permanently equal to 3.

Cenk
  • 1
  • 4
  • 2
    btw, what is the purpose of `Math.floor(6)`? – Nina Scholz Jul 31 '18 at 11:10
  • Yeah `Math.floor`-ing `6` it is unnecessary – shreyasminocha Jul 31 '18 at 11:12
  • where do you use `x`, ...`z`? do you need to know the last values? what about a stack – Nina Scholz Jul 31 '18 at 11:14
  • I don't fully understand your question. Do you want `x`, `y`, `z` to have their own values generated from `randNum()` or do you want them all to have the save values as each other. Because the code you provided makes it so `x`, `y`, `z` have the same values as each other – Nick Parsons Jul 31 '18 at 11:15
  • @Nick Parsons - I would like `x` , `y` , `z` to have their own values generated from `randNum()`. – Cenk Jul 31 '18 at 11:17
  • 1
    Then why not just `x = randNum(); y = randNum(); z = randNum();` – Jonas Wilms Jul 31 '18 at 11:18
  • @Jonas W - You could do that but the `saveDiceNumber()` function will give `x, y, z` a new value every time the `saveDiceNumber()` function is called. What i'm trying to do is have the dice roll once `randNum()` and permanently save the values for `x, y, z`. – Cenk Jul 31 '18 at 11:21
  • ... and isnt that what your code is doing?! – Jonas Wilms Jul 31 '18 at 11:23
  • @Jonas W. - It is, but only temporarily. For example, i've edited the code to have a function where the variables are used. If I was to call the function `saveDiceNumber()` and then the RNG`randNum()` would roll a 3 to which the same function `saveDiceNumber()` would call the function `pickFruit(letter)` to consistently bring back fruit `case(3)` when ever the `saveDiceNumber()` function is called. Thereby permanently saving the value 3 into `var x` via the `randNum()` function. – Cenk Jul 31 '18 at 11:34
  • 1
    (no, i dont understand the edit.) do you need the three variables only inside of `saveDiceNumber`? why is `letter` a number? – Nina Scholz Jul 31 '18 at 11:39
  • @Nina Scholz - I edited the original post again, please refer for more understanding. I need the variables within the `saveDiceNumber()` function to contain separate and permanent values for the letters `w, x, y ,z` which are derived from the dice roll `randNum()` function. `letter` is a number because it recalls the correct `case` from the value given to the variable letters `x, y, z` from the randomly generate number `randNum()` function. – Cenk Jul 31 '18 at 11:48
  • maybe you are looking for global variables. – Nina Scholz Jul 31 '18 at 11:55
  • @Nina Scholz - Sadly, no because it will still give me a random number each time the variable is called. – Cenk Jul 31 '18 at 11:57
  • @Jonas W. Could you please unmark this question as it is not a duplicate? Thank you. – Cenk Jul 31 '18 at 12:18
  • @Jonas W - I understand that but even though it may not be clear for you, it may be clear for someone else. I'm willing to respond to any questions about what is being said to help others come to an understanding, just as I did with your questions. Could you please let others try to answer it before closing it? Thank you. – Cenk Jul 31 '18 at 12:35
  • at which time do you like to set `y and `z` to the random value? what should happen with them (after setting)? – Nina Scholz Jul 31 '18 at 16:10
  • @NinaScholz you are an absolute legend! The fix i needed was to create 2 more random number generators and make the `w, x, y, z` into global variables (like you mentioned before) - Thank you so much! – Cenk Jul 31 '18 at 19:11

1 Answers1

0

Apologies if this question has been very confusing to understand. I have managed to fix what I needed by doing the following:

//global variables
        var x = randNum1();
        var y = randNum2();
        var z = randNum3();

function randNum1(){
    return Math.floor(Math.random()*6); 
}

function randNum2(){
    return Math.floor(Math.random()*6); 
}

function randNum3(){
    return Math.floor(Math.random()*6); 
}

pickFruit(x);
pickFruit(y);
pickFruit(z);
Cenk
  • 1
  • 4