• java随机函数用法Random


     原文地址:http://blog.csdn.net/wpjava/article/details/6004492
     import java.util.Random;
    
    public class RandomNumber{
    
    public static void main(String[] args) {
    
    // 使用java.lang.Math的random方法生成随机数
    System.out.println("Math.random(): " + Math.random());
    
    // 使用不带参数的构造方法构造java.util.Random对象
    System.out.println("使用不带参数的构造方法构造的Random对象:");
    Random rd1 = new Random();
    // 产生各种类型的随机数
    // 按均匀分布产生整数
    System.out.println("int: " + rd1.nextInt());
    // 按均匀分布产生长整数
    System.out.println("long: " + rd1.nextLong());
    // 按均匀分布产生大于等于0,小于1的float数[0, 1)
    System.out.println("float: " + rd1.nextFloat());
    // 按均匀分布产生[0, 1)范围的double数
    System.out.println("double: " + rd1.nextDouble());
    // 按正态分布产生随机数
    System.out.println("Gaussian: " + rd1.nextGaussian());
    
    // 生成一系列随机数
    System.out.print("随机整数序列:");
    for (int i = 0; i < 5; i++) {
    System.out.print(rd1.nextInt() + " ");
    }
    System.out.println();
    
    // 指定随机数产生的范围
    System.out.print("[0,10)范围内随机整数序列: ");
    for (int i = 0; i < 10; i++) {
    // Random的nextInt(int n)方法返回一个[0, n)范围内的随机数
    System.out.print(rd1.nextInt(10) + " ");
    }
    System.out.println();
    System.out.print("[5,23)范围内随机整数序列: ");
    for (int i = 0; i < 10; i++) {
    // 因为nextInt(int n)方法的范围是从0开始的,
    // 所以需要把区间[5,28)转换成5 + [0, 23)。
    System.out.print(5 + rd1.nextInt(23) + " ");
    }
    System.out.println();
    System.out.print("利用nextFloat()生成[0,99)范围内的随机整数序列: ");
    for (int i = 0; i < 10; i++) {
    System.out.print((int) (rd1.nextFloat() * 100) + " ");
    }
    System.out.println();
    System.out.println();
    
    // 使用带参数的构造方法构造Random对象
    // 构造函数的参数是long类型,是生成随机数的种子。
    System.out.println("使用带参数的构造方法构造的Random对象:");
    Random ran2 = new Random(10);
    // 对于种子相同的Random对象,生成的随机数序列是一样的。
    System.out.println("使用种子为10的Random对象生成[0,10)内随机整数序列: ");
    for (int i = 0; i < 10; i++) {
    System.out.print(ran2.nextInt(10) + " ");
    }
    System.out.println();
    Random ran3 = new Random(10);
    System.out.println("使用另一个种子为10的Random对象生成[0,10)内随机整数序列: ");
    for (int i = 0; i < 10; i++) {
    System.out.print(ran3.nextInt(10) + " ");
    }
    System.out.println();
    // ran2和ran3生成的随机数序列是一样的,如果使用两个没带参数构造函数生成的Random对象,
    // 则不会出现这种情况,这是因为在没带参数构造函数生成的Random对象的种子缺省是当前系统时间的毫秒数。
    
    // 另外,直接使用Random无法避免生成重复的数字,如果需要生成不重复的随机数序列,需要借助数组和集合类  
    }
    }运行结果:
    C:/>java RandomNumber
    Math.random(): 0.525171492959965
    使用不带参数的构造方法构造的Random对象:
    int: 636539740
    long: -752663949229005813
    float: 0.87349784
    double: 0.4065973309853902
    Gaussian: 0.4505871918488808
    随机整数序列:1936784917 1339857386 -1185229615 1883411721 1409219372
    [0,10)范围内随机整数序列: 1 1 5 5 9 0 1 0 2 4
    [5,23)范围内随机整数序列: 9 13 26 18 11 27 26 12 21 8
    利用nextFloat()生成[0,99)范围内的随机整数序列: 1 47 72 59 49 86 80 88 55 82
    
    使用带参数的构造方法构造的Random对象:
    使用种子为10的Random对象生成[0,10)内随机整数序列:
    3 0 3 0 6 6 7 8 1 4
    使用另一个种子为10的Random对象生成[0,10)内随机整数序列:
    3 0 3 0 6 6 7 8 1 4 
  • 相关阅读:
    Windows 10 Shell Commands
    scala spark streaming 打印kafka 数据
    Kafka 与 Flume 如何选择
    Scala map中下划线_._2的含义
    Spark中reduceByKey(_+_)的说明
    spark 内存设置
    windows spark3.1 hdfs 测试
    @Autowired、@Resource、和@Service注解详解
    Python 打印对象
    Python + logging 输出到屏幕,将log日志写入文件
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/3487340.html
Copyright © 2020-2023  润新知