Random-number generation
Java have a Math package that can generate random number. Normally this package is used to generate a random with a limit or without a limit.
double randomValue = Math.random();
Math method random generates a random double values in the range from 0.0 up to, but not including, 1.0. But if you want to make in integer or without any decimal place behind there, so you need to manually cast it to int when get the random number.
int randomValue = (int) Math.random();
This will always come out with 0 for the result.So we have to specific what is the maximum random number can generate.
int randomValue = (int) (Math.random*6);
The value of randomValue from the statement above can be from 0 to 5.
Get source code(RandomNumberGenerator.java)
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment