BorisHou
7/16/2018 - 7:26 AM

Random

1、随机一个整数: Random rd = new Random; rd.nextInt(100),产生0-99的整数

package com.borishou.day04;

/*
	1、存储姓名
	2、预览所有人的姓名
	3、随机出一个人的姓名
*/

import java.util.Random;

public class RandomAttendance {
    public static void main(String[] args) {
        //存储姓名,姓名存储到数组中
        //数组存储姓名,姓名的数据类型,String
        String[] names = {"张三","李四","王五"};
        for (String name: names){
            System.out.println(name);
        }

        //随机点名
        Random ran = new Random();
        //随机数范围是0-数组的最大索引
        int index = ran.nextInt(names.length);
        System.out.println(names[index]);

    }

}
package day03;
import java.util.Random;
/**
* Created by Boris Hou on 2018/4/11.
*
* Random类,提供功能,名字 nextInt()产生一个随机数,结果是int类型
* 出现随机数的范围,在功能nextInt(100)
* 范围在0~99
*/

public class RandomDemo02 {
public static void main(String[] args) {
Random rd = new Random();
System.out.println(rd.nextInt(10));

//问题?产生1~100?
System.out.println(rd.nextInt(101)+1);
}
}