Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript-3 random numbers between max and min

I'm generating a random number between min and max with this code:

return min + (max - min) * Math.random();

And it works. However, the random numbers are very little usually between "1 or 3" even if the max is 80.

How can I better distribute the random numbers over all range ?

thanks

like image 858
aneuryzm Avatar asked Apr 18 '10 12:04

aneuryzm


2 Answers

I am very sure that the code you posted, return min + (max - min) * Math.random(); , should return an evenly distributed random number between min (inclusive) and max (exclusive). There is no reason why it would return between 1 and 3.. Did you try tracing min and max to make sure that they are the numbers you think they are?

like image 129
jonathanasdf Avatar answered Sep 20 '22 01:09

jonathanasdf


Math.Random()


Math.random() returns a random number between the values of 0.0 and 1.0. To generate a random integer between 1 and MAX (which i assume U want), Try this:

Math.ceil(Math.random()*MAX);

For More on Math.Random() refer:

Official Actionscript Documentaion

  • To generate a random integer between MIN and MAX, Try this:
    MIN + Math.round(Math.random()*(MAX-MIN));

  • To generate a random decimal (floating-pt number) between MIN and MAX, Try this:
    MIN + Math.random()*(MAX-MIN));

like image 42
TheCodeArtist Avatar answered Sep 19 '22 01:09

TheCodeArtist