java随机产生几个互不相同的数

题目:用java产生1-36中的6个互不相同的整数
解:
class Main {
public void Main(){
int j,i;
boolean flag=true;
int a[] =new int[6];

while(flag==true){
for(j=0;j<6;j++){
a[j]=(int)((6-1+1)*Math.random()+1);
}
for(i=0;i<5;i++){
for(j=i+1;j<6;j++){
if(a[i]==a[j])
flag=true;

else
flag=false;
}
}
}
for(i=0;i<6;i++)
System.out.println(a[i]);
}
}

public class random{
public static void main(String[] args){
Main b=new Main();
b.Main();
}
}

为什么这个程序产生的六个数中会有两个甚至两个以上的相同的数,请高手仔细分析一下这是为什么,并且该怎样改才能使之输出六个互不相同的整数。
分不多,请见谅。
万分感谢!!!
小弟是java初学者,请各位高手用简单的方法说明,因为java中的很多类我都不知道,更不要说使用它们了

我是想得到六个互不相同的数,有些回答的代码会产生几个相同的数。请看清我的问题再回答好么,还有5楼的答案我没有看懂,二楼的方法很好,但是对我这个初学者来说太高深了点。
怎样在我的代码基础上改动一下,使之能完成输出六个互不相同的数这个功能呢?
谁改好了必定给他加分,绝不食言。

package org.java.swingtest;

class Main {
public void Main(){
int a[] = new int[6];
int j = 0;
boolean bool;
while(true)
{
//将bool初始化,每次循环开始,这是一个标志位
bool = false;
//如果已经查出六个数,就结束while循环
if(j == 6)
break;
//用temp变量暂时存储1--36的随机数
int temp = (int)(36*Math.random()+1);//你可以改为 int temp = (int)(6*Math.random()+1);便于测试
//数组的第一位,直接赋值,因为此时数组里还是空的,没有比较对象,j++表示下一位
if(j == 0)
{
a[j] = temp;
j++;
} else //如果不是第一位的情况
{
/* temp临时变量与数组当前位的前面所有位进行比较,例如:当前数组到a[2],temp需要比较a[0],a[1],如果比较temp与a[i]
有相等的情况,说明当前的temp已经在数组中存在了,将bool置为true,可以退出循环
*/
for(int i = 0; i < j; i++)
{
if(temp == a[i])
{
bool = true;
break;
}
}
//bool == true说明此次temp的值不可取,所以继续while循环
if(true == bool)
continue;
else
//bool == false 说明temp的值可取
{
a[j] = temp;
j++;
}
}
}
//输出结果
for(int k =0; k < a.length; k++)
{
System.out.println(a[k]);
}
}
}

public class random{
public static void main(String[] args){
Main b=new Main();
b.Main();
}
}

我把注释写的很清楚了,有问题可以再联系
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-04-09
random就是这样的,上边有一个错误,就是6-1+1应为36-1+1.

有时我们需要同时显示几个随机数。但是他们经常会出现重复,这样狠尴尬。我们就可以用下面的方法防止随机数字重复出现。
例如我们在1~20之间产生5个随机数,显示的时候他们不可重复,可以这样写:

onEnterFrame = function () {//测试用,无关
// 声明两个数组,分别用来储存旧的数组和以后顺序打乱的数组;
a_array = new Array();
b_array = new Array();
var a;
// 声明变量; // 在0~20之间取得数字
for (n=0; n<20; n++) {
// 注意这里是n+1;
a_array[n] = n+1;
}
// 因为要显示5个数字所以重复执行5次
for (k=0; k<5; k++) {
// 随机抽出a_array数组位置;
a = Math.floor(Math.random()*a_array.length);
// 将该位置的元素值反馈给b_array;
b_array[k] = a_array[a];
// 将该位置上的元素从 a_array数组中删除,避免下次再抽中。
a_array.splice(a, 1);
}
// 新的数组产生后,删除a和 a_array
delete a_array;
delete a;
// 动态文本显示新数组
t1 = b_array;
};

1~20之间显示5个数字,不重复效果:
第2个回答  2015-11-16

java随机产生几个互不相同的数,可以通过设置布尔变量数组,如果存在,则置为false,如下代码:

public class MyRandom {
      public static void main(String[] args) {
           int n = 20;
          Random rand = new Random();//新建一个随机类
          boolean[]  bool = new boolean[n];
          int randInt = 0;
          for(int i = 0; i < 6 ; i++) {
               do {
                   randInt  = rand.nextInt(n);//产生一个随机数
               }while(bool[randInt]);
              bool[randInt] = true;
              System.out.println(randInt);
         }
    }
}

第3个回答  2015-11-08
使用Math.rand()函数,然后将其存储在数组中,每生成一个数就与数组中所有的数进行比较,如果相同就进行下一次生成,如果没有相同的就存储到数组中。这样就可以产生几个互不相同的数
第4个回答  2010-04-09
上面写的真多,楼主看看我这个简单的方法
不知道你用没用过Set集合[Set集合里面不允许存放相同的对象]
你可以这样写
public static void main(String[] args) {
Set<Integer> set = new HashSet<Integer>();
while(set.size()<6){
set.add((int)((6-1+1)*Math.random()+1));
}
Object[] ints = set.toArray();
for(int i = 0;i<ints.length;i++){
System.out.println(ints[i]);
}
}本回答被提问者采纳
相似回答