Skip to main content
Inspiring
March 20, 2020
Question

Generate Random Number Multiple Times in AS3?

  • March 20, 2020
  • 1 reply
  • 704 views

I'm doing a random number generator test and I wanted to know if there was a way to generate a random number multiple times all at once. For example, "d4_result" is "d4" generated 1,000 times:

d4 = Math.floor(Math.random() * 4) + 1;
d4_result = d4 * 1000;

But of course, all this does is have "d4" generate one random number from 1-4, and then have that result multiplied 1000 times. I want "d4_result" to be 1000 random results of "d4". Is this possible? Currently my only workaround is by having a countdown timer with a max of 1,000 frames, and having "d4" generate a number one frame at a time, each additional frame adding to "d4_result," and although this works, you obviously have to wait that many frames to get the final value rather than instantly. Any ideas? Thank you!!

 

    This topic has been closed for replies.

    1 reply

    Brainiac
    March 21, 2020

    I take it you're unfamiliar with the programming concept of loops.

    https://www.geeksforgeeks.org/loops-in-javascript/

     

    var d4_result = 0;
    var i;
    for (i = 0; i < 1000; i++) {
         d4_result += Math.floor(Math.random() * 4) + 1;
    }

     

     

    Inspiring
    March 21, 2020

    Oh yeah. That would work. 🙂

     

    Thanks!