黑马程序员技术交流社区

标题: 求随机数,但不能有重复的元素 [打印本页]

作者: 爱你一万年    时间: 2016-8-21 09:25
标题: 求随机数,但不能有重复的元素
随机生成1-10之间5个随机数,但不能有重复的,并将结果打印到控制台,求大神给一点思路!
作者: 695212308    时间: 2016-8-21 10:37
public class Test {
        public static void main(String[] args) {
                int[] arr=new int[10];
                arr[0]=(int)(Math.random()*(10))+1;
                a:for(int i=1;i<=arr.length/2;i++){
                        outer:while(true){
                                int a=(int)(Math.random()*(10))+1;
                                for(int x=0;x<i;x++){
                                        if(a==arr[x]){
                                                continue outer;
                                        }
                                }
                                arr[i]=a;
                                continue a;
                        }
                }
                for(int i=0;i<arr.length/2;i++){
                        System.out.println(arr[i]);
                }
        }
}
作者: 胡eason    时间: 2016-8-21 10:54
加油!加油!加油!加油!
作者: Maroon    时间: 2016-8-21 13:08
695212308 发表于 2016-8-21 10:37
public class Test {
        public static void main(String[] args) {
                int[] arr=new int[10];

用HashSet更简单啊
作者: q2450751976    时间: 2016-8-21 13:10
public static void main(String[] args){
                                Random ra = new Random();
                                int a [] = new int [10] ;
                                //每产生一个随机数都和之前产生的去比对
                                //若重复则不赋值
                                aa:
                                for(int i =0 ;i<9;i++){
                                        a[i] =ra.nextInt(10)+1;
                                        for(int j = 0; j<i;j++){
                                                if(a[i]==a[j]){    // 产生的数a[i] 跟0到i索引 去比较
                                                        i--;        //若有相等的 则 本次赋值无效 重新赋值
                                                continue aa;
                                                }
                                        }       
                                }
                                System.out.print(Arrays.toString(a));//输出数组 a 的值
                }
作者: 小小丶白    时间: 2016-8-21 16:04
[AppleScript] 纯文本查看 复制代码
        public static void main(String[] args) {
                HashSet<Integer> hs = new HashSet<Integer>();
                Random random = new Random();
                while (hs.size() < 5) {
                        //获取一个1-10之间的随机数
                        int temp = random.nextInt(9) + 1;
                        hs.add(temp);
                }
                for (Integer integer : hs) {
                        System.out.print(integer + "  ");
                }
        }

作者: 小小丶白    时间: 2016-8-21 20:45
小小丶白 发表于 2016-8-21 16:04
[mw_shl_code=applescript,true]        public static void main(String[] args) {
                HashSet hs = new HashSet() ...

那就把值改为10就好了,思路在那
作者: 菜菜_f9490    时间: 2016-8-21 21:14
推荐 Hash的集合来用没有重复值
作者: bin931207    时间: 2016-8-22 00:24
HashSet是你最好的伙伴
作者: ancheng    时间: 2016-8-28 08:56
小小丶白 发表于 2016-8-21 16:04
[mw_shl_code=applescript,true]        public static void main(String[] args) {
                HashSet hs = new HashSet() ...

int temp = random.nextInt(10) + 1;
作者: 小超超    时间: 2016-8-28 13:14
产生随机数,然后判断本次随机数是否与上次随机数相等。
作者: yaozequan    时间: 2016-8-28 13:20
将 random产生的数据用HashSet或者TreeSet集合存储,其中TreeSet还有自动排序功能。
作者: 细听风语为梧桐    时间: 2016-8-28 17:43
hashset 和math 类中的 random 结合




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2