2020-08-20

抽号系统

使用Random与集合来产生不重复的数

思想

1.先使用死循环再用Random来创建规定数据的大小的抽号池,不能规定次数,因为不确定每次抽到的都是不重复的

   while (true)
   random = r.nextInt(33);

2.使用Random方法nextInt来抽取小于最大规定数的号码

int nextInt(int bound) 
返回伪随机的,均匀分布 int值介于0(含)和指定值(不包括),从该随机数生成器的序列绘制。  码片

3.使用if循环来规定最小号码,如果小于最小号码则退出当前循环

 if (random<1){
                continue;
            }

4.使用list中的contains来判断是否在集合中已近有这个数值,如果没有则使用add加入

if (!list.contains(random)){
                list.add(random);
            }

5.使用list中的size方法判断数量是否已达到规定数量

  if (list.size()==6){
                break;
            }

6.最终打印

System.out.println(list.toString());

使用集合的代码

public class Random {
    public static void main(String[] args) {
        int count = 0;
        int random = 0;
        List list = new ArrayList();
        java.util.Random r = new java.util.Random();
        while (true){
            random = r.nextInt(33);
            if (random<1){
                continue;
            }
            if (!list.contains(random)){
                list.add(random);
            }
            if (list.size()==6){
                break;
            }
        }
        System.out.println(list.toString());
    }

}

使用Random与for来产生不重复的数

思想

1.创建集合和数组

 LinkedList<Integer>  list  =  new  LinkedList<Integer>();
 int[]  result  =  new  int[count];
2.使用for来循环范围类的数,并全部加入到集合中

```java
  for  (int  i  =  1;  i  <=  range  ;  i++)  {
                        list.add(i);
                }

3.将已经存入的数据进行打乱

 Collections.shuffle(list);

系统shuffle方法源码

 public static void shuffle(List<?> list) {
        Random rnd = r;
        if (rnd == null)
            r = rnd = new Random(); // harmless race.
        shuffle(list, rnd);
    }

shuffle方法解释

<p>This method runs in linear time.  If the specified list does not
     * implement the {@link RandomAccess} interface and is large, this
     * implementation dumps the specified list into an array before shuffling
     * it, and dumps the shuffled array back into the list.  This avoids the
     * quadratic behavior that would result from shuffling a "sequential
     * access" list in place.
     * <p >此方法在线性时间内运行。如果指定的列表不
     *实现{@link RandomAccess}接口,并且很大,这
     *实现在洗牌之前将指定的列表转储到数组中
     *它,并将混洗的数组转储回列表中。这避免了
     *洗牌会导致的二次行为
     *访问"列表到位。

4.循环已近打乱的数据,并提取出需要的几个数据(已近打乱直接取出即使打乱后的数据)

   for  (int  i  =  0;  i  <result.length  ;  i++)  {
               result[i]  =  list.pop();
                }

5.返回

Arrays.sort(result);
                return  result;

使用for循环代码

public static int[] generRandom(int range,int count){
LinkedList list = new LinkedList();
int[] result = new int[count];
for (int i = 1; i <= range ; i++) {
list.add(i);
}
Collections.shuffle(list);
for (int i = 0; i <result.length ; i++) {
result[i] = list.pop();
}
Arrays.sort(result);
return result;
}


附言:Java中集合很重要,集合也不是太难,所以很建议学好集合。亲身经历。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值