获取N个不重复的随机数,获取n随机数,/**返回的long型的
分享于 点击 31558 次 点评:233
获取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; }
用户点评