How do you use Math.random to generate random ints?
My code is:
int abc= (Math.random()*100);
System.out.println(abc);
All it prints out is 0, how can I fix this?
Random rand = new Random(); int x = rand. nextInt(10); x will be between 0-9 inclusive. So, given the following array of 25 items, the code to generate a random number between 0 (the base of the array) and array.
random() The Math. random() function returns a floating-point, pseudo-random number that's greater than or equal to 0 and less than 1, with approximately uniform distribution over that range — which you can then scale to your desired range.
Cast abc to an integer.
(int)(Math.random()*100);
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