【JavaSE】生成1-10随机数


random.nextInt() Math.random()

import java.util.Random;

public class randomTest {
    
    
    public static void main(String[] args) {
    
    

        // 生成随机数
        // 方法一:nextInt()
        //     返回一个伪随机数,均匀分布的{@code int}值*在0(含)和指定值(不含)之间,取自此随机数生成器的序列
        Random r = new Random();
        int num1 = r.nextInt(10)+1;
        System.out.println(num1);

        // 方法二:Math.random()
        //     返回带有正号的{@code double}值,该值大于*等于或等于{@code 0.0}并且小于{@code 1.0}
        int num2 = (int) (Math.random()*10+1);
        System.out.println(num2);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_45797116/article/details/115007744