# java 随机数

Random Numbers

The method you need to use isMath.Random();. This produces a random number from 0 to 1. To make this useful, you need to multiply by the range, and add the lowest number.

int thing = (int)(Math.random()*5);This produces a whole random number from 0 to 4int thing = (int)(Math.random()*10+1);This produces a whole random number from 1 to 10double thing = (Math.random()*5+1);This produces a real random number from 1 to 5

Random numbers aren't actually random, but are generated by complicated maths.

If you need a random sequence that is the same every time the program is run, then the Random class is more useful.

static Random bob = new Random(); Creates a new Random() object.bob.setSeed(12345); Sets a seed, of type long ***check long*** + codingSystem.out.println("The next random number is :" + bob.nextDouble());System.out.println("The one after that is :" + bob.nextDouble());

NOTE: The Java section is mostly in maintenence mode. I don't have time to work on it right now. Errors will be corrected if pointed out, but they are not actively being searched for. Newer site features, like alternate stylesheets, may cause problems with these pages.

• 评论

• 上一篇
• 下一篇