Java – how to use math Random() gets the random number in the range
•
Java
See English answer > math random,only generating a 0? 5
public class Tester{ public static void main(String args[]){ int size=52; while(size>0){ int rando=(int)Math.random()*size; size--; System.out.println(rando); } } }
My code prints out all zeros until the conditions of the while loop are met I want to know how to generate random integers between 0 and 52 I understand, math random(); There is a double, I think, type conversion problem thank you.
Solution
You can only project math random(). Its value is between 0 and 1 (excluding 1)
Throw out the whole expression: (int) (math. Random() * size);
BTW: your interval is only 0 to 51 (because 1.0 is excluded)
Use (int) (math. Random() * (size 1));, If you want 0... 52 as your interval
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码