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

产生随机数

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

449
产生随机数
//来 自 时代Java公众号 - nowjava.com

import java.util.Random;


public class Main {


  public void main(String[] argv) {

    Random rand = new Random();


    // Random integers

    int i = rand.nextInt();

    // Continually call nextInt() for more random integers ...


    // Random integers that range from from 0 to n

    int n = 10;

    i = rand.nextInt(n + 1);


    // Random bytes

    byte[] bytes = new byte[5];

    rand.nextBytes(bytes);

    /** 
     来自 N o w  J a v a  .   c o m**/

    // Other primitive types

    boolean b = rand.nextBoolean();

    long l = rand.nextLong();

    float f = rand.nextFloat(); // 0.0 <= f < 1.0

    double d = rand.nextDouble(); // 0.0 <= d < 1.0


    
展开阅读全文