如何生成6位不重复的随机数?

yin_yjing 2009-11-05 04:41:48
如标题,用java噢
...全文
25425 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuai1234 2011-11-08
  • 打赏
  • 举报
回复
四楼算法,视乎有问题吧。换成String更好一些吧。你那样的话,”012345----12345“第一位是0的话只有5位肯定不对的。
dingchengcheng 2011-06-29
  • 打赏
  • 举报
回复
java.util.Random random=new Random();
int temp[]=new int[6];
for(int i=0;i<6;i++ )
{
temp[i]=random.nextInt(60);
if(temp[i]==0)
{
i--;
continue;
}
for(int k=0;k<i;k++)
{

if(temp[i]==temp[k])
{
i--;
break;
}
}

}

for(int out=0;out<temp.length;out++)
System.out.println(temp[out]);

}
yin_yjing 2009-11-09
  • 打赏
  • 举报
回复
可能大家不太明白我的意思,我的意思是每次生成的6位数的6各数字可以重复,但是多次生成的6位数跟以前生成的不要重复,例如生成1万各6位数都不相同
lovebin_bin 2009-11-09
  • 打赏
  • 举报
回复
public void general2(){
List<Integer> list = new ArrayList<Integer>();
Random random = new Random();
while (true) {
int randomNum = random.nextInt(10);
boolean flag = false;
for(Integer in : list){
if(in == randomNum){
flag = true;
break;
}
}
if(!flag){
list.add(randomNum);
}
if(list.size()>=6){
break;
}
}
String randomStr = "";
for (Integer in : list) {
randomStr += in.toString();
}
System.out.println(randomStr);
}

6位不同的随机数
ChDw 2009-11-09
  • 打赏
  • 举报
回复
不是都说了将曾经生成过的6位数放入HashSet中,随机生成一个数时先判断这个Set中是否存在,如果存在重新再生成一个新的随机数
Set<Integer> set = new HashSet<Integer>();
Random rand = new Random();
for(int i = 0; i < 10000; i++)
while(!set.add(rand.nextInt(900000) + 100000));


这种是适合生成个数远远小于rand可能个数(即6位数的总个数)情况

如果生成个数非常多,则应该先生成所有可能数,再利用我上面的那种交换位置算法
zkqprotoss 2009-11-06
  • 打赏
  • 举报
回复
6楼没看清题吧。。。是单次的6位数,每位不同
按你的说法,随机数总有重复的时候
  • 打赏
  • 举报
回复
6 位的话,运行 100 万次肯定有一次是重复的。

所以该问题无解!
filemon 2009-11-06
  • 打赏
  • 举报
回复
四楼正解,这是最优算法的。
以前我也一直在想这个问题,这个是在Java Core中看到的,有一个例子就是这样写的。
ChDw 2009-11-06
  • 打赏
  • 举报
回复
		int[] array = {0,1,2,3,4,5,6,7,8,9};
Random rand = new Random();
for (int i = 10; i > 1; i--) {
int index = rand.nextInt(i);
int tmp = array[index];
array[index] = array[i - 1];
array[i - 1] = tmp;
}
int result = 0;
for(int i = 0; i < 6; i++)
result = result * 10 + array[i];
System.out.println(result);
yin_yjing 2009-11-06
  • 打赏
  • 举报
回复
要的是每次生成的6位数都不相同
shengchuang 2009-11-06
  • 打赏
  • 举报
回复
利用set集合就行了.
说明白点吧,int i = random.nextInt(10);
用个循环,把每一次随即生成的i放入到set里,直到他的size=6;
它里面的6个值一定不会重复了
shengchuang 2009-11-06
  • 打赏
  • 举报
回复
利用set集合就行了
wanli209 2009-11-06
  • 打赏
  • 举报
回复
d
WdWheyr 2009-11-06
  • 打赏
  • 举报
回复
public class RandomUtils
{
private static String[] randomValues = new String[]{"0","1","2","3","4","5","6","7","8","9",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","u",
"t","s","o","x","v","p","q","r","w","y","z"};
public static String getUsername(int lenght)
{
StringBuffer str = new StringBuffer();
for(int i = 0;i < lenght; i++)
{
Double number=Math.random()*(randomValues.length-1);
str.append(randomValues[number.intValue()]);
}

return str.toString();
}

public static void main(String[] args)
{
System.out.println(RandomUtils.getUsername(6));
}
}
这样就能随机生成六个不同的数了
dengganxia 2009-11-06
  • 打赏
  • 举报
回复
4楼我看懂了
ChDw 2009-11-06
  • 打赏
  • 举报
回复
如果你指批量随机数,那就用一个Set保存已经获取过的。如果已经存在就再取

不过这种做法,在生成数量非常多的时候效率很低,只适合生成小量的
ChDw 2009-11-06
  • 打赏
  • 举报
回复
你的题目是指生成一个6位数,保证每位数字都不相同吧?

算法当然是没有问题,怎么可能会重复!!算法是交换位置,不可能出现两位相同的数字的
littlemonster 2009-11-05
  • 打赏
  • 举报
回复
Random rand= new Random();
Set<Integer> set= new HashSet<Integer>();
while (set.size() < 3)
set.add(rand.nextInt(30));
goosman 2009-11-05
  • 打赏
  • 举报
回复
Random是java.util包下的
Random random = new Random();
static Map tmp = new HashMap();
public static int generate() {
int i = random.nextInt(100000);
try {
return tmp.get(i) == null ? generate() : i;
} finally {
tmp.put(i, "");
}
}

50,535

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧