APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.random

public final Class RandomGeneratorFactory<T extends RandomGenerator>

extends Object
Class Inheritance
Type Parameters
<T>
type of created random generator
Imports
jdk.internal.random.L128X1024MixRandom, .L128X128MixRandom, .L128X256MixRandom, .L32X64MixRandom, .L64X1024MixRandom, .L64X128MixRandom, .L64X128StarStarRandom, .L64X256MixRandom, .Xoroshiro128PlusPlus, .Xoshiro256PlusPlus, java.math.BigInteger, java.security.SecureRandom, java.util.Objects, .Map, .Random, .SplittableRandom, java.util.concurrent.ThreadLocalRandom, java.util.random.RandomGenerator.ArbitrarilyJumpableGenerator, .RandomGenerator.JumpableGenerator, .RandomGenerator.LeapableGenerator, .RandomGenerator.SplittableGenerator, .RandomGenerator.StreamableGenerator, java.util.stream.Stream

This is a factory class for generating multiple random number generators of a specific algorithm. RandomGeneratorFactory also provides methods for selecting random number generator algorithms. A specific RandomGeneratorFactory can be located by using the RandomGeneratorFactory#of(String) method, where the argument string is the name of the algorithm required. The method RandomGeneratorFactory#all() produces a non-empty Stream of all available RandomGeneratorFactorys that can be searched to locate a RandomGeneratorFactory suitable to the task. There are three methods for constructing a RandomGenerator instance, depending on the type of initial seed required. RandomGeneratorFactory#create(long) is used for long seed construction, RandomGeneratorFactory#create(byte[]) is used for byte[] seed construction, and RandomGeneratorFactory#create() is used for random seed construction. Example;
RandomGeneratorFactory<RandomGenerator> factory = RandomGeneratorFactory.of("Random"); for (int i = 0; i < 10; i++) { new Thread(() -> { RandomGenerator random = factory.create(100L); System.out.println(random.nextDouble()); }).start(); }
RandomGeneratorFactory<RandomGenerator> factory = RandomGeneratorFactory.of("Random");

 for (int i = 0; i < 10; i++) {
     new Thread(() -> {
         RandomGenerator random = factory.create(100L);
         System.out.println(random.nextDouble());
     }).start();
 }
RandomGeneratorFactory also provides methods describing the attributes (or properties) of a generator and can be used to select random number generator algorithms. These methods are typically used in conjunction with RandomGeneratorFactory#all(). In this example, the code locates the RandomGeneratorFactory that produces RandomGenerators with the highest number of state bits.
RandomGeneratorFactory<RandomGenerator> best = RandomGeneratorFactory.all() .filter(rgf -> !rgf.name().equals("SecureRandom")) // SecureRandom has MAX_VALUE stateBits. .sorted(Comparator.comparingInt(RandomGeneratorFactory<RandomGenerator>::stateBits).reversed()) .findFirst() .orElse(RandomGeneratorFactory.of("Random")); System.out.println(best.name() + " in " + best.group() + " was selected"); RandomGenerator rng = best.create(); System.out.println(rng.nextLong());
RandomGeneratorFactory<RandomGenerator> best = RandomGeneratorFactory.all()
    .filter(rgf -> !rgf.name().equals("SecureRandom")) // SecureRandom has MAX_VALUE stateBits.
    .sorted(Comparator.comparingInt(RandomGeneratorFactory<RandomGenerator>::stateBits).reversed())
    .findFirst()
    .orElse(RandomGeneratorFactory.of("Random"));
System.out.println(best.name() + " in " + best.group() + " was selected");

RandomGenerator rng = best.create();
System.out.println(rng.nextLong());
Since
17
See Also
java.util.random

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static record

Field Summary

Modifier and TypeField and Description
private static final String
private final RandomGeneratorFactory.RandomGeneratorProperties
properties

Random generator properties.

Constructor Summary

AccessConstructor and Description
private
RandomGeneratorFactory(RandomGeneratorFactory.RandomGeneratorProperties
Random generator properties.
properties
)

Private constructor.

Method Summary

Modifier and TypeMethod and Description
public static Stream<RandomGeneratorFactory<RandomGenerator>>

Returns:

a non-empty stream of all available RandomGeneratorFactory(s).
all
()

Returns a non-empty stream of available RandomGeneratorFactory(s).

public T

Returns:

new instance of RandomGenerator.
create
()

Create an instance of RandomGenerator based on the algorithm chosen.

public T

Returns:

new instance of RandomGenerator.
create
(long
long random seed value.
seed
)

Create an instance of RandomGenerator based on the algorithm chosen, and the provided seed.

public T

Returns:

new instance of RandomGenerator.
create
(byte[]
byte array random seed value.
seed
)

Create an instance of RandomGenerator based on the algorithm chosen, and the provided seed.

public int

Returns:

the equidistribution of the algorithm.
equidistribution
()

Returns the equidistribution of the algorithm.

pack-priv static <
Sub-interface of RandomGenerator to produce
T extends RandomGenerator
>
RandomGeneratorFactory<T>

Returns:

Factory of RandomGenerator
factoryOf
(String
Name of random number algorithm to use
name
,
Class<T>
Sub-interface of RandomGenerator to type check
category
)

Returns a RandomGeneratorFactory that will produce instances of RandomGenerator that utilizes the named algorithm.

private static RandomGeneratorFactory.RandomGeneratorProperties

Returns:

A RandomGeneratorProperties instance matching name and category.
findClass
(String
Name of RandomGenerator
name
,
Class<? extends RandomGenerator>
Interface category, sub-interface of RandomGenerator.
category
)

Returns a RandomGeneratorProperties instance matching name and category.

public static RandomGeneratorFactory<RandomGenerator>
getDefault()

Returns a RandomGeneratorFactory meeting the minimal requirement of having an algorithm whose state bits are greater than or equal 64.

private static Map<String, RandomGeneratorFactory.RandomGeneratorProperties>

Returns:

Map of random generator classes.
getFactoryMap
()

Returns the factory map, lazily constructing the map on first call.

public String

Returns:

Group name of the algorithm.
group
()

Return the group name of the algorithm used by the random number generator.

public boolean

Returns:

true if random generator is arbitrarily jumpable.
isArbitrarilyJumpable
()

Return true if random generator can jump an arbitrarily specified distant point in the state cycle.

public boolean

Returns:

true if the implementation of RandomGenerator (algorithm) has been marked for deprecation
isDeprecated
()

Return true if the implementation of RandomGenerator (algorithm) has been marked for deprecation.

public boolean

Returns:

true if random generator is generated by hardware.
isHardware
()

Return true if random generator uses a hardware device (HRNG) to produce entropic input.

public boolean

Returns:

true if random generator is jumpable.
isJumpable
()

Return true if random generator can jump a specified distant point in the state cycle.

public boolean

Returns:

true if random generator is leapable.
isLeapable
()

Return true if random generator is jumpable and can leap to a very distant point in the state cycle.

public boolean

Returns:

true if random generator is splittable.
isSplittable
()

Return true if random generator can be cloned into a separate object with the same properties but positioned further in the state cycle.

public boolean

Returns:

true if random generator is statistical.
isStatistical
()

Return true if random generator is computed using an arithmetic algorithm and is statistically deterministic.

public boolean

Returns:

true if random generator is stochastic.
isStochastic
()

Return true if random generator is computed using external or entropic sources as inputs.

public boolean

Returns:

true if random generator is streamable.
isStreamable
()

Return true if random generator can be used to create Streams of random numbers.

private boolean

Returns:

true if the random generator class is a subclass of the category.
isSubclass
(Class<? extends RandomGenerator>
Interface category, sub-interface of RandomGenerator.
category
)

Return true if the random generator class is a subclass of the category.

private static boolean

Returns:

true if rgClass is a subclass of the category.
isSubclass
(Class<? extends RandomGenerator>
Interface category, sub-interface of RandomGenerator.
category
,
Class<? extends RandomGenerator>
Class that is being filtered.
rgClass
)

Return true if rgClass is a subclass of the category.

public String

Returns:

Name of the algorithm.
name
()

Return the name of the algorithm used by the random number generator.

pack-priv static <
Sub-interface of RandomGenerator to produce
T extends RandomGenerator
>
T

Returns:

An instance of RandomGenerator
of
(String
Name of random number algorithm to use
name
,
Class<T>
Sub-interface of RandomGenerator to type check
category
)

Returns a RandomGenerator that utilizes the name algorithm.

public static <
Sub-interface of RandomGenerator to produce
T extends RandomGenerator
>
RandomGeneratorFactory<T>
of(String
Name of random number generator algorithm
name
)

Returns a RandomGeneratorFactory that can produce instances of RandomGenerator that utilize the name algorithm.

public BigInteger

Returns:

BigInteger period.
period
()

Return the period of the algorithm used by the random number generator.

public int

Returns:

number of bits used by the algorithm to maintain state of seed.
stateBits
()

Returns number of bits used by the algorithm to maintain state of seed.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_ALGORITHMback to summary
private static final String DEFAULT_ALGORITHM
propertiesback to summary
private final RandomGeneratorFactory.RandomGeneratorProperties properties

Random generator properties.

Constructor Detail

RandomGeneratorFactoryback to summary
private RandomGeneratorFactory(RandomGeneratorFactory.RandomGeneratorProperties properties)

Private constructor.

Parameters
properties:RandomGeneratorFactory.RandomGeneratorProperties

Random generator properties.

Method Detail

allback to summary
public static Stream<RandomGeneratorFactory<RandomGenerator>> all()

Returns a non-empty stream of available RandomGeneratorFactory(s). RandomGenerators that are marked as deprecated are not included in the result.

Returns:Stream<RandomGeneratorFactory<RandomGenerator>>

a non-empty stream of all available RandomGeneratorFactory(s).

createback to summary
public T create()

Create an instance of RandomGenerator based on the algorithm chosen.

Returns:T

new instance of RandomGenerator.

createback to summary
public T create(long seed)

Create an instance of RandomGenerator based on the algorithm chosen, and the provided seed. If the RandomGenerator doesn't support instantiation through a seed of type long then this method throws an UnsupportedOperationException.

Parameters
seed:long

long random seed value.

Returns:T

new instance of RandomGenerator.

Exceptions
UnsupportedOperationException:
if a seed of type long in not supported.
createback to summary
public T create(byte[] seed)

Create an instance of RandomGenerator based on the algorithm chosen, and the provided seed. If the RandomGenerator doesn't support instantiation through a seed of type byte[] then this method throws an UnsupportedOperationException.

Parameters
seed:byte[]

byte array random seed value.

Returns:T

new instance of RandomGenerator.

Exceptions
UnsupportedOperationException:
if a seed of type byte[] in not supported.
NullPointerException:
if seed is null.
equidistributionback to summary
public int equidistribution()

Returns the equidistribution of the algorithm.

Returns:int

the equidistribution of the algorithm.

factoryOfback to summary
pack-priv static <T extends RandomGenerator> RandomGeneratorFactory<T> factoryOf(String name, Class<T> category) throws IllegalArgumentException

Returns a RandomGeneratorFactory that will produce instances of RandomGenerator that utilizes the named algorithm.

Parameters
<T>
Sub-interface of RandomGenerator to produce
name:String

Name of random number algorithm to use

category:Class<T>

Sub-interface of RandomGenerator to type check

Returns:RandomGeneratorFactory<T>

Factory of RandomGenerator

Exceptions
IllegalArgumentException:
when either the name or category is null
findClassback to summary
private static RandomGeneratorFactory.RandomGeneratorProperties findClass(String name, Class<? extends RandomGenerator> category) throws IllegalArgumentException

Returns a RandomGeneratorProperties instance matching name and category.

Parameters
name:String

Name of RandomGenerator

category:Class<? extends RandomGenerator>

Interface category, sub-interface of RandomGenerator.

Returns:RandomGeneratorFactory.RandomGeneratorProperties

A RandomGeneratorProperties instance matching name and category.

Exceptions
IllegalArgumentException:
if the resulting type is not a subclass of category.
getDefaultback to summary
public static RandomGeneratorFactory<RandomGenerator> getDefault()

Returns a RandomGeneratorFactory meeting the minimal requirement of having an algorithm whose state bits are greater than or equal 64.

Implementation Specification

Since algorithms will improve over time, there is no guarantee that this method will return the same algorithm over time.

Returns:RandomGeneratorFactory<RandomGenerator>

a RandomGeneratorFactory

getFactoryMapback to summary
private static Map<String, RandomGeneratorFactory.RandomGeneratorProperties> getFactoryMap()

Returns the factory map, lazily constructing the map on first call.

Returns:Map<String, RandomGeneratorFactory.RandomGeneratorProperties>

Map of random generator classes.

groupback to summary
public String group()

Return the group name of the algorithm used by the random number generator.

Returns:String

Group name of the algorithm.

isArbitrarilyJumpableback to summary
public boolean isArbitrarilyJumpable()

Return true if random generator can jump an arbitrarily specified distant point in the state cycle.

Returns:boolean

true if random generator is arbitrarily jumpable.

isDeprecatedback to summary
public boolean isDeprecated()

Return true if the implementation of RandomGenerator (algorithm) has been marked for deprecation.

Implementation Note

Random number generator algorithms evolve over time; new algorithms will be introduced and old algorithms will lose standing. If an older algorithm is deemed unsuitable for continued use, it will be marked as deprecated to indicate that it may be removed at some point in the future.

Returns:boolean

true if the implementation of RandomGenerator (algorithm) has been marked for deprecation

isHardwareback to summary
public boolean isHardware()

Return true if random generator uses a hardware device (HRNG) to produce entropic input.

Returns:boolean

true if random generator is generated by hardware.

isJumpableback to summary
public boolean isJumpable()

Return true if random generator can jump a specified distant point in the state cycle.

Returns:boolean

true if random generator is jumpable.

isLeapableback to summary
public boolean isLeapable()

Return true if random generator is jumpable and can leap to a very distant point in the state cycle.

Returns:boolean

true if random generator is leapable.

isSplittableback to summary
public boolean isSplittable()

Return true if random generator can be cloned into a separate object with the same properties but positioned further in the state cycle.

Returns:boolean

true if random generator is splittable.

isStatisticalback to summary
public boolean isStatistical()

Return true if random generator is computed using an arithmetic algorithm and is statistically deterministic.

Returns:boolean

true if random generator is statistical.

isStochasticback to summary
public boolean isStochastic()

Return true if random generator is computed using external or entropic sources as inputs.

Returns:boolean

true if random generator is stochastic.

isStreamableback to summary
public boolean isStreamable()

Return true if random generator can be used to create Streams of random numbers.

Returns:boolean

true if random generator is streamable.

isSubclassback to summary
private boolean isSubclass(Class<? extends RandomGenerator> category)

Return true if the random generator class is a subclass of the category.

Parameters
category:Class<? extends RandomGenerator>

Interface category, sub-interface of RandomGenerator.

Returns:boolean

true if the random generator class is a subclass of the category.

isSubclassback to summary
private static boolean isSubclass(Class<? extends RandomGenerator> category, Class<? extends RandomGenerator> rgClass)

Return true if rgClass is a subclass of the category.

Parameters
category:Class<? extends RandomGenerator>

Interface category, sub-interface of RandomGenerator.

rgClass:Class<? extends RandomGenerator>

Class that is being filtered.

Returns:boolean

true if rgClass is a subclass of the category.

nameback to summary
public String name()

Return the name of the algorithm used by the random number generator.

Returns:String

Name of the algorithm.

ofback to summary
pack-priv static <T extends RandomGenerator> T of(String name, Class<T> category) throws IllegalArgumentException

Returns a RandomGenerator that utilizes the name algorithm.

Parameters
<T>
Sub-interface of RandomGenerator to produce
name:String

Name of random number algorithm to use

category:Class<T>

Sub-interface of RandomGenerator to type check

Returns:T

An instance of RandomGenerator

Exceptions
IllegalArgumentException:
when either the name or category is null
ofback to summary
public static <T extends RandomGenerator> RandomGeneratorFactory<T> of(String name)

Returns a RandomGeneratorFactory that can produce instances of RandomGenerator that utilize the name algorithm.

Parameters
<T>
Sub-interface of RandomGenerator to produce
name:String

Name of random number generator algorithm

Returns:RandomGeneratorFactory<T>

RandomGeneratorFactory of RandomGenerator

Exceptions
NullPointerException:
if name is null
IllegalArgumentException:
if the named algorithm is not found
periodback to summary
public BigInteger period()

Return the period of the algorithm used by the random number generator. Returns BigInteger.ZERO if period is not determinable.

Returns:BigInteger

BigInteger period.

stateBitsback to summary
public int stateBits()

Returns number of bits used by the algorithm to maintain state of seed.

Returns:int

number of bits used by the algorithm to maintain state of seed.

java.util.random back to summary

private final Record RandomGeneratorFactory.RandomGeneratorProperties

extends Record
Class Inheritance
Record Components
rgClass:Class<? extends RandomGenerator> 
name:String 
group:String 
i:int 
j:int 
k:int 
equidistribution:int 
flags:int 

Field Summary

Modifier and TypeField and Description
private static final int
private static final int
private static final int
private final int
equidistribution

Record Component accessed by equidistribution().

private static final Map<String, RandomGeneratorFactory.RandomGeneratorProperties>
private final int
flags

Record Component accessed by flags().

private final String
group

Record Component accessed by group().

private static final int
private final int
i

Record Component accessed by i().

private static final int
private final int
j

Record Component accessed by j().

private final int
k

Record Component accessed by k().

private static final int
private final String
name

Record Component accessed by name().

private final Class<? extends RandomGenerator>
rgClass

Record Component accessed by rgClass().

private static final int

Constructor Summary

AccessConstructor and Description
private
RandomGeneratorProperties(Class<? extends RandomGenerator> rgClass, String name, String group, int i, int j, int k, int equidistribution, int flags)

Method Summary

Modifier and TypeMethod and Description
private RandomGenerator
private RandomGenerator
create(long seed)

private RandomGenerator
create(byte[] seed)

private static Map<String, RandomGeneratorFactory.RandomGeneratorProperties>

Returns:

Map of RandomGeneratorProperties.
createFactoryMap
()

Returns the factory map, lazily constructing it on first use.

private static int
deprecationBit(Class<? extends RandomGenerator> rgClass)

private static Map.Entry<String, RandomGeneratorFactory.RandomGeneratorProperties>
entry(Class<? extends RandomGenerator> rgClass, String name, String group, int i, int j, int k, int equidistribution, int flags)

public final boolean
equals(Object o)

Implements abstract java.lang.Record.equals.

Indicates whether some other object is "equal to" this one.

public int
equidistribution()

Record Component getter of equidistribution.

public int
flags()

Record Component getter of flags.

public String
group()

Record Component getter of group.

public final int
hashCode()

Implements abstract java.lang.Record.hashCode.

Returns a hash code value for the record.

public int
i()

Record Component getter of i.

private boolean
private boolean
private boolean
private boolean
public int
j()

Record Component getter of j.

public int
k()

Record Component getter of k.

public String
name()

Record Component getter of name.

private BigInteger
public Class<? extends RandomGenerator>
rgClass()

Record Component getter of rgClass.

private int
public final String
toString()

Implements abstract java.lang.Record.toString.

Returns a string representation of the record.

Field Detail

ALL_CONSTRUCTORSback to summary
private static final int ALL_CONSTRUCTORS
BYTE_ARRAY_SEEDback to summary
private static final int BYTE_ARRAY_SEED
DEPRECATEDback to summary
private static final int DEPRECATED
equidistributionback to summary
private final int equidistribution

Record Component accessed by equidistribution().

FACTORY_MAPback to summary
private static final Map<String, RandomGeneratorFactory.RandomGeneratorProperties> FACTORY_MAP
flagsback to summary
private final int flags

Record Component accessed by flags().

groupback to summary
private final String group

Record Component accessed by group().

HARDWAREback to summary
private static final int HARDWARE
iback to summary
private final int i

Record Component accessed by i().

INSTANTIABLEback to summary
private static final int INSTANTIABLE
jback to summary
private final int j

Record Component accessed by j().

kback to summary
private final int k

Record Component accessed by k().

LONG_SEEDback to summary
private static final int LONG_SEED
nameback to summary
private final String name

Record Component accessed by name().

rgClassback to summary
private final Class<? extends RandomGenerator> rgClass

Record Component accessed by rgClass().

STOCHASTICback to summary
private static final int STOCHASTIC

Constructor Detail

RandomGeneratorPropertiesback to summary
private RandomGeneratorProperties(Class<? extends RandomGenerator> rgClass, String name, String group, int i, int j, int k, int equidistribution, int flags)

Method Detail

createback to summary
private RandomGenerator create()
createback to summary
private RandomGenerator create(long seed)
createback to summary
private RandomGenerator create(byte[] seed)
createFactoryMapback to summary
private static Map<String, RandomGeneratorFactory.RandomGeneratorProperties> createFactoryMap()

Returns the factory map, lazily constructing it on first use.

Although ThreadLocalRandom can only be accessed via ThreadLocalRandom#current(), a map entry is added nevertheless to record its properties that are otherwise not documented anywhere else.

Currently, no algorithm is deprecated.

Returns:Map<String, RandomGeneratorFactory.RandomGeneratorProperties>

Map of RandomGeneratorProperties.

deprecationBitback to summary
private static int deprecationBit(Class<? extends RandomGenerator> rgClass)
entryback to summary
private static Map.Entry<String, RandomGeneratorFactory.RandomGeneratorProperties> entry(Class<? extends RandomGenerator> rgClass, String name, String group, int i, int j, int k, int equidistribution, int flags)
equalsback to summary
public final boolean equals(Object o)

Implements abstract java.lang.Record.equals.

Doc from java.lang.Record.equals.

Indicates whether some other object is "equal to" this one. In addition to the general contract of Object.equals, record classes must further obey the invariant that when a record instance is "copied" by passing the result of the record component accessor methods to the canonical constructor, as follows:

    R copy = new R(r.c1(), r.c2(), ..., r.cn());
then it must be the case that r.equals(copy).
equidistributionback to summary
public int equidistribution()

Record Component getter of equidistribution.

flagsback to summary
public int flags()

Record Component getter of flags.

groupback to summary
public String group()

Record Component getter of group.

hashCodeback to summary
public final int hashCode()

Implements abstract java.lang.Record.hashCode.

Doc from java.lang.Record.hashCode.

Returns a hash code value for the record. Obeys the general contract of Object.hashCode. For records, hashing behavior is constrained by the refined contract of Record.equals, so that any two records created from the same components must have the same hash code.

iback to summary
public int i()

Record Component getter of i.

isDeprecatedback to summary
private boolean isDeprecated()
isHardwareback to summary
private boolean isHardware()
isInstantiableback to summary
private boolean isInstantiable()
isStochasticback to summary
private boolean isStochastic()
jback to summary
public int j()

Record Component getter of j.

kback to summary
public int k()

Record Component getter of k.

nameback to summary
public String name()

Record Component getter of name.

periodback to summary
private BigInteger period()
rgClassback to summary
public Class<? extends RandomGenerator> rgClass()

Record Component getter of rgClass.

stateBitsback to summary
private int stateBits()
toStringback to summary
public final String toString()

Implements abstract java.lang.Record.toString.

Doc from java.lang.Record.toString.

Returns a string representation of the record. In accordance with the general contract of Object#toString(), the toString method returns a string that "textually represents" this record. The result should be a concise but informative representation that is easy for a person to read.

In addition to this general contract, record classes must further participate in the invariant that any two records which are equal must produce equal strings. This invariant is necessarily relaxed in the rare case where corresponding equal component values might fail to produce equal strings for themselves.