Random class
A generator of random bool, int, 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.new([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
-
nextBool(
) → bool -
Generates a random boolean value.
override
-
nextDouble(
) → double -
Generates a non-negative random floating point value uniformly distributed
in the range from 0.0, inclusive, to 1.0, exclusive.
override
-
nextInt(
int max) → int -
Generates a non-negative random integer uniformly distributed in the range
from 0, inclusive, to
max
, exclusive.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited