yooli88

[转帖]在数组里随机取不重复的数

方法没有确认
import java.util.*;
public class Lottery {

    public static void main(String[] args) {
        int[] data1 = {3, 5, 6, 8, 9, 15, 18, 24, 27, 30, 32};
        Random r = new Random();
        int irdm = 0;
        for(int i = 0; i < 7; i ++) {
            irdm = r.nextInt(11 - i);
            System.out.println(data1[irdm]);
            for(int j = irdm; j < 11 - i - 1; j ++) {
                data1[j] = data1[j + 1];
            }
        }
    }
}
方法没有确认不知道是否可行public class AAAAA {
        public static void main(String[] args) {

                int i = 0;
                int j = 0;
                int[] temp = new int[20];
                for (j = 0; j < temp.length; j++) {
                        temp[j] = Math.random() * 100  + 1;
                        System.out.print(temp[j] + ",";
                }
                
                HashSet hh = new HashSet();
                while (hh.size() < 7) {
                        int aa = (int) (Math.random() * 100 + 1);
                        hh.add(aa);
                }
                System.out.println(hh.size());
                Iterator ii = hh.iterator();
                while (ii.hasNext()) {
                        System.out.print(ii.next() + ",";
                }
        }

}

java从指定数组中取不重复的7个随机数

如何用java从指定数组中取不重复的7个随机数,以下是我写的代码,但是是有重复的,哪位大侠赐教一下如何使用Random类的种子,帮我实现不重复的随机数。。。
import java.util.*;
public class Lottery {

    public static void main(String[] args)
    {
        int[] data1 = {3,5,6,8,9,15,18,24,27,30,32};
        Random   r=new   Random();
        StringBuffer   str1=new   StringBuffer();
        for(int   i=0;i<6;i++)
        {
        str1.append(data1[r.nextInt(11)]);
         }
         System.out.println(str1);
   }
}

posted on 2011-01-31 16:57 迷茫在java的世界里 阅读(432) 评论(0)  编辑  收藏 所属分类: 吸人精华 吐俺的糟粕 我吸(吸星大法 hoho~~~)


只有注册用户登录后才能发表评论。


网站导航: