?
很难吗?请看Java.util.Random

解决方案 »

  1.   

    package test.wzq;import java.util.Random;public final class Temp {    public static void main(String[] args) {
            // Base Array. Use for save number from 1 to 10.
            int [] baseArray = new int [10];
            // Set data to array.
            for(int i=0; i<10; i++) {
                baseArray[i] = i;
            }
            // Use system time as the seed of Random
            Random random = new Random(System.currentTimeMillis());
            // Get first value.
            int firstIndex = random.nextInt(10);
            System.out.println("The first value is :" + baseArray[firstIndex]);
            // remove the first value from array.
            for(int i=firstIndex; i<9; i++) {
                baseArray[i] = baseArray[i + 1];
            }
            int secondIndex = random.nextInt(9);
            System.out.println("The second value is :" + baseArray[secondIndex]);
        }
    }不好意思,没写中文注释……
      

  2.   

    Random
    nextInt
    public int nextInt(int n)Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence. The general contract of nextInt is that one int value in the specified range is pseudorandomly generated and returned. All n possible int values are produced with (approximately) equal probability. The method nextInt(int n) is implemented by class Random as follows: 
     public int nextInt(int n) {
         if (n<=0)
    throw new IllegalArgumentException("n must be positive");     if ((n & -n) == n)  // i.e., n is a power of 2
             return (int)((n * (long)next(31)) >> 31);     int bits, val;
         do {
             bits = next(31);
             val = bits % n;
         } while(bits - val + (n-1) < 0);
         return val;
     }
     The hedge "approximately" is used in the foregoing description only because the next method is only approximately an unbiased source of independently chosen bits. If it were a perfect source of randomly chosen bits, then the algorithm shown would choose int values from the stated range with perfect uniformity. The algorithm is slightly tricky. It rejects values that would result in an uneven distribution (due to the fact that 2^31 is not divisible by n). The probability of a value being rejected depends on n. The worst case is n=2^30+1, for which the probability of a reject is 1/2, and the expected number of iterations before the loop terminates is 2. The algorithm treats the case where n is a power of two specially: it returns the correct number of high-order bits from the underlying pseudo-random number generator. In the absence of special treatment, the correct number of low-order bits would be returned. Linear congruential pseudo-random number generators such as the one implemented by this class are known to have short periods in the sequence of values of their low-order bits. Thus, this special case greatly increases the length of the sequence of values returned by successive calls to this method if n is a small power of two.Parameters:
    n - the bound on the random number to be returned. Must be positive.
    Returns:
    a pseudorandom, uniformly distributed int value between 0 (inclusive) and n (exclusive).
      

  3.   

    给你一个例子
    Random rm = new Random();
    int n1 = rm.nextInt(10); //返回从0到9一个随机整数
    int n2 = rm.nextInt();   //返回一个随机整数
      

  4.   

    import java.util.Random;public class Rand{
    public static void main(String[] args){
    Random rd =new Random();
    int result=0;
    int temp=0;
    int[] num1={0,1,2,3,4,5,6,7,8,9};
    int[] num2=new int[9];

    result=rd.nextInt(10); //0---9
    System.out.println("the first: "+num1[result]);
    for(int i=0;i<10;i++){
    if(i<result){
    num2[i]=num1[i];
    }else if(i>result){
    num2[i-1]=num1[i];
    }//end if
    }//end for
    result=rd.nextInt(9);
    System.out.println("the second: "+num2[result]);
    }
    }
      

  5.   

    int dd = (int)(Math.random()*10);