面试:数组:产生随机数

给一个整数数组,以及min和max,写一个函数产生一个在[min, max] 之间的随机数。该随机数不再数组内部。

import java.util.*;

public class Solution {


    public void numInRange(int[] intList,int min,int max){
        Arrays.sort(intList); //对输入数据排序
        int range=max-min+1;
        Random rand=new Random();
        int result=rand.nextInt(range)+min;
        //二分查找
        while(Arrays.binarySearch(intList, result)>=0)
            result=rand.nextInt(range)+min;

        System.out.println(result);

    }


    public static void main(String[] args){

        Solution s=new Solution();
        int min=1;
        int max=20;
        int[] test={6,8,9,5,7,10};
        s.numInRange(test,min,max);

    }
}

你可能感兴趣的:(面试:数组:产生随机数)