Random

 1 package day04;
 2 
 3 import java.util.Random;
 4 
 5 public class RandomDemo {
 6     /*Random:产生随机数
 7     使用步骤:
 8     1、导包:import java.util.Random;导包的语句必须写在类定义的上面
 9     2、创建对象:Random ran = new Random();ran是变量名可以变,其他不能变
10     3、获取随机数:int number = ran.nextInt(10);number是变量名,可以变。
11     数字10可以变,其他不能变。获取的数据范围:[0,10),包括0,不包括10。
12 
13     * */
14     public static void main(String[] args) {
15         Random ran = new Random();
16         for (int i = 1; i <= 10; i++) {
17             int num = ran.nextInt(10)+1;
18             System.out.println(num);
19         }
20     }
21 }

执行结果:

欢迎批评指正,提出问题,谢谢!
原文地址:https://www.cnblogs.com/xxeleanor/p/14209108.html