Highlighted

## Generate Random Number Multiple Times in AS3?

Explorer ,
Mar 20, 2020

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

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

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

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
2 Replies 2
Mar 20, 2020

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

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
LATEST
Explorer ,
Mar 20, 2020

Copied

Oh yeah. That would work. 🙂

Thanks!

Likes

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