文章目录 一、作用二、使用步骤1.创建对象2.生成随机数3.演示案例 一、作用 通过 Random 类来生成随机数。 二、使用步骤 1.创建对象 Random r = new Random(); 2.生成随机数 int num = r.nextInt(10);//[0,9] 3.演示案例 //导包 import java.util.Random; public class RandomDemo { public static void main(String[] args) { Random r = new Random(); int num = r.nextInt(10)+ 1;//[1,10] System.out.println(num); } }