用Math.random()产生各个数位不同的随机数,例如4位数。

解决方案 »

  1.   

    math.random()产生的是0~1之间的数,你后面乘以10000就ok了
      

  2.   

    1000+(int)(9000*Math.random()) 产生的是1000到9999的随机数
      

  3.   


    用Math貌似不能达到这个效果,但用Random这个类可以
      

  4.   

    public class Test {
    static int[] arr = new int[4]; public static void main(String args[]) {
    int i = 0;
    while (i < 4) {
    if (i == 0)
    arr[i] = rand();
    else {
    arr[i] = rand();
    while(a(i)) {// 如果有相等的則继续给第重新生成,直到没有相等
    arr[i] = rand();
    }
    }
    i++;
    }
    for(int a:arr){
    System.out.print(a);
    }
    } // 判断是否有何前面的相等
    public static boolean a(int m) {
    boolean flag = false;
    for (int i = m-1; i >= 0; i--) {
    if (arr[i] == arr[m])
    flag = true;
    }
    return flag;
    } public static int rand() {
    return (int) ((Math.random() * 9) + 1);
    }
    }
      

  5.   

    以前写过猜数字游戏的一个功能和这个类似 public String getDigit(){  
    10.        Random r=new Random();  
    11.        int n=0;  
    12.        while(n<SIZE){  
    13.            String temp="";  
    14.            int i=r.nextInt(10);  
    15.            temp+=(char)('0'+i);  
    16.            if(!digit.contains(temp)){  
    17.                digit+=(char)('0'+i);  
    18.                n++;  
    19.            }  
    20.        }  
    21.        return digit;  
    22.    }  这是生成4个不同数字,但是他是字符串,希望对楼主有帮助
      

  6.   

    java.util.concurrent
    Class ThreadLocalRandomjava.lang.Object
    java.util.Random
    java.util.concurrent.ThreadLocalRandom
    All Implemented Interfaces:
    Serializablepublic class ThreadLocalRandom
    extends Random
    A random number generator isolated to the current thread. Like the global Random generator used by the Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.
    Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads.This class also provides additional commonly used bounded random generation methods.Since:
    1.7
    See Also:
    Serialized Form
    Method SummaryMethods 
    Modifier and Type Method and Description
    static ThreadLocalRandom current()
    Returns the current thread's ThreadLocalRandom.
    protected int next(int bits)
    Generates the next pseudorandom number.
    double nextDouble(double n)
    Returns a pseudorandom, uniformly distributed double value between 0 (inclusive) and the specified value (exclusive).
    double nextDouble(double least, double bound)
    Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
    int nextInt(int least, int bound)
    Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
    long nextLong(long n)
    Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).
    long nextLong(long least, long bound)
    Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
    void setSeed(long seed)
    Throws UnsupportedOperationException.
      

  7.   

    4位数由没有讲究?比如第一个数字不能是0?List<Integer> list = new LinkedList<Integer>();
    for (int i=0; i<10; i++) {
        list.add(i); //把数字0-9放入集合
    }
    //取第一个不为0的数字
    //因为上面知道0在头位置,所以随机取1位置以后的一个数字
    int index = (int)(Math.random()*(list.size()-1)) + 1; //随机位置
    int data = list.remove(index); //取出随机位置的数字,并把数字从集合中删除,这样再取就不会重复
    Collections.shuffle(list); 随机打乱list的元素
    for (int i=0; i<3, i++) { //从集合中取3个数字
        data = data*10 + list.get(i); 
    }
    System.out.println(data);
      

  8.   

    (int)(9000*Math.random()) + 1000
      

  9.   

    可以将产生的随即数放进一个容器里如ArrayList。然后将后面产生的随机数用集合的contains();函数来判断一下前面有没有用过。用过就丢弃这个随机数,没用过则就放到集合里。最后够位数了就顺序从集合里取出来合成一个整数。实现起来应该比上面那么长代码的简单些。
      

  10.   

    System.out.println(new DecimalFormat("####").format(Math.random()*10000));