How do I get a random value without decimal point ?
Develop games in your browser. Powerful, performant & highly capable.
Try using int(x) where x is the expression you use to get a random value.
or round(random(X)) ;)
Rounding can make the probabilities uneven - round(random(2)) will give you:
0.0 to 0.5: 0
0.5 to 1.5: 1
1.5 to 2.0: 2
1 is twice as likely to come up as 0 or 2.
Thank you ! <img src="smileys/smiley4.gif" border="0" align="middle" />
yes thats true, but isn't random act like that? you can have 1 everytime, or never. You never know, thats pure luck ;P
If you use random usually you want equal weighting, i.e. each number is as likely as any other to come up. For example:
int(random(2)) will give:
0 (50% of the time)
1 (50% of the time)
Note both have an equal chance of appearing.
round(random(2)) will give:
0 (25% of the time)
2 (25% of the time)
Not only is the range different, but different numbers have different chances of appearing. In the long run you'll get twice as many 1's as you probably expected. Like I say, usually you want all numbers to appear roughly as many times as each other in the long run, so int() or floor() are probably better here.
Can't beat that explanation :D
I ussually use floor(random).
Maybe has the same effect that int()
It nearly does - floor() returns a float of the number rounded down, and int() returns the number rounded down and converted to an integer. For most uses though they're identical.