当前位置: 首页 > news >正文

Java 21的Pseudorandom的笔记

Linux系统下,常见的随机数生成设备:

  • /dev/random
    随机性比较好,依赖内核的中断,当中断数量不足,获取随机数的操作会被阻塞。通过安装haveged,可以提升熵值。
    JDK的默认选项。

  • /dev/urandom
    通过熵池来产生随机数,牺牲一定的随机性,解决性能问题。
    启动Java应用时,增加如下选项,指定使用/dev/urandom作为随机数生成器。

    -Djava.security.egd=file:/dev/./urandom
    

文档

  • Java Core Libraries

  • Pseudorandom Number Generators
    PRNGs即Pseudorandom Number Generators。

  • Characteristics of PRNGs

  • Generating Pseudorandom Numbers with RandomGenerator Interface
    使用RandomGenerator来获取随机数,样例代码,如下:

    RandomGenerator random1 = RandomGenerator.of("Random");
    long value1 = random1.nextLong();
    System.out.println(value1);
    

    使用RandomGeneratorFactory来创建随机数生成器,并获取随机数,样例代码,如下:

    RandomGeneratorFactory<RandomGenerator> factory2 =RandomGeneratorFactory.of("SecureRandom");
    RandomGenerator random2 = factory2.create();
    long value2 = random2.nextLong();
    System.out.println(value2);
    

    使用Random来获取随机数,样例代码,如下:

    Random random = new Random();
    int randomNumber = random.nextInt();
    System.out.println(randomNumber);
    

    使用SecureRandom来获取随机数,样例代码,如下:

    SecureRandom r1 = new SecureRandom();
    SecureRandom r2 = SecureRandom.getInstance("NativePRNG");
    SecureRandom r3 = SecureRandom.getInstance("DRBG",DrbgParameters.instantiation(128, RESEED_ONLY, null));
    

    RandomGenerator的几个子类,如下:

    • RandomGenerator.StreamableGenerator
    • RandomGenerator.SplittableGenerator
    • RandomGenerator.LeapableGenerator
    • RandomGenerator.JumpableGenerator
    • RandomGenerator.ArbitrarilyJumpableGenerator
  • Generating Pseudorandom Numbers in Multithreaded Applications

  • Dynamically Creating New Generators

  • Creating Stream of Generators

  • Choosing a PRNG Algorithm
    通过使用RandomGenerator.isDeprecated()或者RandomGeneratorFactory.isDeprecated(),可以检查随机数生成算法是否被Java官方推荐使用。
    当前可用的随机数生成算法:

    • L64X128MixRandom
    • Xoroshiro128PlusPlus
    • L32X64StarStarRandom
    • L32X64MixRandom
    • Xoshiro256PlusPlus
    • L64X256MixRandom
    • MRG32k3a
    • L128X128MixRandom
    • L128X256MixRandom
    • L64X1024MixRandom
    • L128X1024MixRandom

    选择随机数生成器时,平衡如下因素:

    • speed,生成随机数的速度
    • space,算法运行时占用的空间
    • period,随机数的生成周期

参考资料

  • Java获取/dev/urandom的随机数
  • java dev urandom_/dev/random和/dev/urandom的一点备忘
  • /dev/urandom和/dev/random的区别
  • linux 手动安装移植 haveged,解决随机数初始化慢的问题
  • 熵值低于1000,需要启动haveged
  • haveged的代码仓库

    Additionally, since v5.6, as soon as the CRNG (the Linux cryptographic-strength random number generator) gets ready, /dev/random does not block on reads anymore.

  • haveged的官方主页
  • 在Java中,可以使用java.util.Random类来生成随机数,使用Java生成随机数的示例代码
  • 【Java代码审计】失效认证及不安全随机数篇
  • Procedual Generation in Minecraft - Structure Generation

http://www.mrgr.cn/news/24099.html

相关文章:

  • 校园二手数码交易系统小程序的设计
  • 如何在Flask中实现用户认证
  • Python | Leetcode Python题解之第398题随机数索引
  • css的选择器有哪些?权重由大到小是怎么排序的?
  • AI学习指南深度学习篇-自适应矩估计(Adam)简介
  • vue的自定义指令
  • Ubuntu 24.04 配置 nginx + php-fpm
  • C++ 在项目中使用Git
  • 【玩转贪心算法专题】406. 根据身高重建队列【中等】
  • 详细分析Redis常用命令(图文)
  • vue自定义指令
  • C语言 ——— 学习并使用 #if defined #ifdef #ifndef 条件编译指令
  • Ionic 头部和底部
  • 第 8 章图像内容分类
  • Apache License 2.0 和 MIT License 区别
  • 从数据洞察到智能决策:合合信息infiniflow RAG技术的实战案例分享
  • Linux环境常用的一些网络相关的命令
  • Selenium面试题(二)
  • Android大厂高频面试题解析,Android面试题及解析
  • Matlab -- meshgrid和peaks的用法