集册 Java实例教程 随机产生值

随机产生值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

618
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用java.util.Random类可帮助生成随机数。
/**
nowjava.com - 时  代  Java
**/


import java.util.Random;


public class Main {


  public static void main(String[] args) {

    // Create a new instance of the Random class

    Random random = new Random();

    System.out.println("Random: " + random);


    // Generates a random Integer

    int myInt = random.nextInt();
    /**
    来 自 时代Java公众号 - N o w J a  v a . c o m
    **/

    System.out.println("Random int: " + myInt);

    // Generates a random Double value

    double myDouble = random.nextDouble();

    System.out.println("Random double: " + myDouble);


    // Generates a random float

    float myFloat = random.nextFloat();

    System.out.println("Random float: " + myFloat);


    // Generates a random Gaussian double

    // mean 0.0 and standard deviation 1.0

    double gausDouble = random.nextGaussian();

    System.out.println("Random Gaussian double: " + gausDouble);

    // Generates a random Long

    long myLong = random.nextLong();

    System.out.println("Random long: " + myLong);

    // Generates a random boolean

  
展开阅读全文