Java 17 Recipes
-8. Pseudorandom Number Generators
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- How It Works
2-8. Pseudorandom Number Generators
Problem You want to create random numbers. Solution Java 17 improves the functionality by providing new interface types and implementations for pseudorandom number generators (PRNGs) and preserving previous behavior of the java.util.Random class. An example is the creation of a pseudorandom integer number. import java.util.random.RandomGenerator; import java.util.Random; public class UseNewPRNGs implements RandomGenerator { static UseNewPRNGs testGen = new UseNewPRNGs(); public static void main(String[] args){ for (int i = 0; i < 5; i++) { System.out.println(testGen.nextBoolean()); System.out.println(testGen.nextDouble()); Chapter 2 enhanCements from Java 9 through Java 17 61 System.out.println(testGen.nextInt()); System.out.println(testGen.nextLong()); } } @Override public long nextLong() { //Returns a pseudorandomly chosen long value. Random r= new Random(); return r.nextLong(); } } The following is the output. false 0.4134883035327678 1576006117 7534183938614386691 true 0.37651481128199904 682788707 -6435840544183136500 true 0.8508900370304876 833362345 45614195681877909 true 0.10116382711133465 -663894214 -3718926050146429223 false 0.01988848947697064 1224659827 -2167276196617947918 Chapter 2 enhanCements from Java 9 through Java 17 62 How It Works Java 17 provides a new interface, RandomGenerator, which supplies a uniform API for all existing and new PRNGs. It has default methods, and in the previous code was only overridden the nextLong method. It provides a common protocol for objects that generate random or pseudorandom sequences of numbers or Boolean values. This enhancement mainly aims to use various PRNG algorithms interchangeably in applications and preserve the existing behavior of the java.util.Random class. For more information on PRNGs, see the documentation at https://openjdk.java .net/jeps/356 . Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling