java 随机数

[url]http://www.kirun.co.uk/java/answers/33.php[/url]
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 4
int thing = (int)(Math.random()*10+1);This produces a whole random number from 1 to 10
double 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*** + coding
System.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.

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭