So far I have this code
int w = (int)((450-150)*random()+150);
This generates a number between 450 and 150... But I have no idea how to make that number a multiple of 10.
If you want to create random numbers in the range of integers in Java than best is to use random. nextInt() method it will return all integers with equal probability. You can also use Math. random() method to first create random number as double and than scale that number into int later.
For example, to generate a random number between 1 and 10, we can do it like below. ThreadLocalRandom random = ThreadLocalRandom. current(); int rand = random. nextInt(1, 11);
Easy. Just generate random numbers between 15 and 45...then multiply by 10:
int w = ((int)((45 - 15) * random() + 15)) * 10;
Pick a random number between 45 and 15 and multiply it with 10 -
int w = (int)((45-15)*random()+15) * 10;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With