Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Generate Random Number Multiple Times in AS3?

Explorer ,
Mar 20, 2020 Mar 20, 2020

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!!

 

518
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 20, 2020 Mar 20, 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;
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 20, 2020 Mar 20, 2020
LATEST

Oh yeah. That would work. 🙂

 

Thanks!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines