Highlighted

Generate Random Number Multiple Times in AS3?

Explorer ,
Mar 20, 2020

Copy link to clipboard

Copied

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

 

Views

96

Likes

Translate

Translate

Report

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

Generate Random Number Multiple Times in AS3?

Explorer ,
Mar 20, 2020

Copy link to clipboard

Copied

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

 

Views

97

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 20, 2020 0
Adobe Community Professional ,
Mar 20, 2020

Copy link to clipboard

Copied

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;
}

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 20, 2020 0
Explorer ,
Mar 20, 2020

Copy link to clipboard

Copied

Oh yeah. That would work. 🙂

 

Thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 20, 2020 0