欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

获取N个不重复的随机数,获取n随机数,/**返回的long型的

来源: javaer 分享于  点击 28671 次 点评:73

获取N个不重复的随机数,获取n随机数,/**返回的long型的


/**返回的long型的最大值是startLongValue+count-1     * @param needCount     * @param count     * @param startLongValue     * @return     */    public static List<Long> randomNoRepeatLongArray(int needCount,int count,long startLongValue){        //这种情况 会出现无限循环的        if(needCount>count) return null;        Random random = new Random();        int[] ints = new int[count];        for(int i=0;i<count;i++){            ints[i]=i;        }        List<Long> list = new ArrayList<Long>();        while(list.size()<needCount){            int temp = random.nextInt(count);            if(ints[temp] != -1){                list.add(temp+startLongValue);                ints[temp]=-1;            }        }        ints = null;        return list;    }
相关栏目:

用户点评