Random class

A generator of random byte, bool, int, BigInt, or double values.

The default implementation supplies a stream of pseudo-random bits that are not suitable for cryptographic purposes.

Use the Random.secure constructor for cryptographic purposes.

Example 1: To create a non-negative random integer uniformly distributed in the range from 0, inclusive, to max, exclusive, use nextInt(int max).

var intValue = Random().nextInt(10); // Value is >= 0 and < 10.
intValue = Random().nextInt(100) + 50; // Value is >= 50 and < 150.

Example 2: To create a non-negative random floating point value uniformly distributed in the range from 0.0, inclusive, to 1.0, exclusive, use nextDouble

var doubleValue = Random().nextDouble(); // Value is >= 0.0 and < 1.0.
doubleValue = Random().nextDouble() * 256; // Value is >= 0.0 and < 256.0.

Example 3: To create a random Boolean value, use nextBool.

var boolValue = Random().nextBool(); // true or false, with equal chance.
Implemented types

Constructors

Random([int? seed])
Random.secure()
Creates a cryptographically secure random number generator.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

nexBigInt2(BigInt max) BigInt
Generates a random BigInt less than max using direct byte accumulation.
nextBigInt(BigInt max) BigInt
Generates a random BigInt less than max using hex string parsing.
nextBigIntInRange(BigInt min, BigInt max) BigInt
Generates a random BigInt in the range [min, max). Ensures the generated BigInt is within the specified range.
nextBigIntWithBitLength(int bitLength) BigInt
Generates a random BigInt with a specified bit length.
nextBool() bool
Generates a random boolean value.
override
nextBytes(int length) Uint8List
Generates a list of length random bytes.
nextBytesInRange(int min, int max, int length) Uint8List
Generates a list of random bytes within a specified range.
nextDateTime(DateTime min, DateTime max) DateTime
Generates a random DateTime within the given range.
nextDouble() double
Generates a non-negative random floating point value uniformly distributed in the range from 0.0, inclusive, to 1.0, exclusive.
override
nextDoubleInRange(double min, double max) double
Generates a random double in the range [min, max).
nextElementFromList<T>(List<T> list) → T
Randomly selects an element from the provided list.
nextGaussian({double mean = 0.0, double stddev = 1.0}) double
Generates a random number from a Gaussian (normal) distribution.
nextInt(int max) int
Generates a non-negative random integer uniformly distributed in the range from 0, inclusive, to max, exclusive.
override
nextIntInRange(int min, int max) int
Generates a random integer in the range [min, max).
nextNonRepeatingIntList(int min, int max, int length) List<int>
Generates a list of unique random integers within a specified range.
nextNonZeroByte() int
Generates a random non-zero byte.
nextString(int length, {String charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'}) String
Generates a random string of length characters from the given charset.
nextUUID() String
Generates a random UUID (version 4).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
shuffleList<T>(List<T> list) List<T>
Shuffles a list in place using the Fisher-Yates shuffle algorithm.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited