Java基础面试操作题: 获取 1-20 之间的随机数,共计 20 个,要求不能重复 获取 1-20 之间的随机数,共计 10 个,要求不能重

package com.swift;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Suijishu_Test {

    public static void main(String[] args) {
        /*
         * 获取 1-20 之间的随机数,共计 20 个,要求不能重
         */

        Random ran = new Random();
        Set<Integer> set = new HashSet<Integer>();
        int num;
        for (int i = 0; i < 100; i++) {
            num = ran.nextInt(20) + 1;
            System.out.println(num);
                set.add(num);
        }
        System.out.println();
        for(Integer i:set) {
            System.out.print(i+"~");
        }
    }

}

 获取1~20间的随机数 共计10个,要求不能重复

package com.swift;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Suijishu_Test {

    public static void main(String[] args) {
        /*
         * 获取 1-20 之间的随机数,共计 10 个,要求不能重
         */

        Random ran = new Random();
        Set<Integer> set = new HashSet<Integer>();
        int num;
        for (int i = 0; i < 100; i++) {
            num = ran.nextInt(20) + 1;
            System.out.println(num);
                set.add(num);
                if(set.size()>=10) {
                    break;
                }
        }
        System.out.println();
        for(Integer i:set) {
            System.out.print(i+"~");
        }
    }

}

 使用另一种方法获得随机数,System.currentTimeMIllis()

package com.swift;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Suijishu_Test {

    public static void main(String[] args) {
        /*
         * 获取 1-20 之间的随机数,共计 10 个,要求不能重
         */

        Set<Integer> set = new HashSet<Integer>();
        int num;
        for (int i = 0; i < 100; i++) {
            num =(int) (System.currentTimeMillis() % 20+1);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(num);
            set.add(num);
            if (set.size() >= 10) {
                break;
            }
        }
        System.out.println();
        for (Integer i : set) {
            System.out.print(i + "~");
        }
    }

}

使用Math类的random方法

package com.swift;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class Suijishu_Test {

    public static void main(String[] args) {
        /*
         * 获取 1-20 之间的随机数,共计 10 个,要求不能重
         */

        Set<Integer> set = new HashSet<Integer>();
        int num;
        for (int i = 0; i < 100; i++) {
            num =(int) (Math.random()*20+1);
            System.out.println(num);
            set.add(num);
            if (set.size() >= 10) {
                break;
            }
        }
        System.out.println();
        for (Integer i : set) {
            System.out.print(i + "~");
        }
    }

}

 

posted @ 2018-01-21 13:15  Advancing-Swift  阅读(1380)  评论(0编辑  收藏  举报