import java.util.Random;public class Test {
public static void main(String[] args) {
Random ra = new Random(100);
System.out.println(ra.nextInt());
}
}
为什么得到的不是100之内的数呢?应该是我没理解,请各位指点  谢谢~~

解决方案 »

  1.   

    Random ra = new Random();//构造函数里传的是随机种子
    System.out.println(ra.nextInt(100))
      

  2.   


    这个我在提问之前已经试过,也知道应该是这样,但不明白:Random ra = new Random(100);还有啥用?
      

  3.   

    楼上的说的不知道对不对
    我记得Thinking in java书上说只有用47作为种子,每次才会产生相同的随机数
    Random ra = new Random(47);读取一个100以内的随机数要用ra.nextInt(100);
      

  4.   

    nextInt()与nextInt(int)方法里都调用了next()方法,next方法是这样的 
    synchronized protected int next(int bits) { 
    seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1); 
    return (int)(seed >>> (48 - bits)); 

    里面的seed变量就是种子,Random rand =new Random(100); 实际调用了setSeed(long)这个方法,从而使seed这样成员变量赋值为25。 
    随机数生成器对象的状态由seed控制,里面比较复杂的随机数的均衡算法反正一直没用到,暂时当它没什么用~
      

  5.   

    If you create a Random object with no arguments, Java uses the current time as a seed for the random number generator, and will thus produce different output for each execution of the program. However, in the examples in this book, it is important that the output shown at the end of the examples be as consistent as possible, so that this output can be verified with external tools. By providing a seed (an initialization value for the random number generator that will always produce the same sequence for a particular seed value) when creating the Random object, the same random numbers will be generated each time the program is executed, so the output is verifiable.1 To generate more varying output, feel free to remove the seed in the examples in the book. 
    选自《编程思想
      

  6.   

    Random r = new Random();
    System.out.println(r.nextInt(100))
      

  7.   

    呵呵,今天刚看了视频
    应该改成
    Random ra = new Random();
    System.out.println(ra.nextInt(100));
    ra只生成一个Random类的实例。
      

  8.   

    如果想每次运行结果一样:
    那么,
    Random ra = new Random(常量值);
    System.out.println(ra.nextInt(100));
    如果想每次运行不一样:
    那么,
    Random ra = new Random();
    System.out.println(ra.nextInt(100));ra.nextInt(100)表示随机返回0~100之内的整数(包括0,但不包括100)