Skip to content

Commit 50db7c5

Browse files
jpsiitonenCommit Bot
authored and
Commit Bot
committed
Documentation update for Random class
Examples added Closes #47986 #47986 GitOrigin-RevId: ecb3a42 Change-Id: I3c3a406482be6cb55bb01aba4f78c74ae2d7648f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/225120 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]>
1 parent 225b8d3 commit 50db7c5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

sdk/lib/math/random.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ part of dart.math;
1010
/// not suitable for cryptographic purposes.
1111
///
1212
/// Use the [Random.secure]() constructor for cryptographic purposes.
13+
///
14+
/// To create a non-negative random integer uniformly distributed in the range
15+
/// from 0, inclusive, to max, exclusive, use [nextInt(int max)].
16+
/// ```dart
17+
/// var intValue = Random().nextInt(10); // Value is >= 0 and < 10.
18+
/// intValue = Random().nextInt(100) + 50; // Value is >= 50 and < 150.
19+
/// ```
20+
/// To create a non-negative random floating point value uniformly distributed
21+
/// in the range from 0.0, inclusive, to 1.0, exclusive, use [nextDouble].
22+
/// ```dart
23+
/// var doubleValue = Random().nextDouble(); // Value is >= 0.0 and < 1.0.
24+
/// doubleValue = Random().nextDouble() * 256; // Value is >= 0.0 and < 256.0.
25+
/// ```
26+
/// To create a random Boolean value, use [nextBool].
27+
/// ```dart
28+
/// var boolValue = Random().nextBool(); // true or false, with equal chance.
29+
/// ```
30+
///
1331
abstract class Random {
1432
/// Creates a random number generator.
1533
///
@@ -29,12 +47,29 @@ abstract class Random {
2947
///
3048
/// Implementation note: The default implementation supports [max] values
3149
/// between 1 and (1<<32) inclusive.
50+
///
51+
/// Example:
52+
/// ```dart
53+
/// var intValue = Random().nextInt(10); // Value is >= 0 and < 10.
54+
/// intValue = Random().nextInt(100) + 50; // Value is >= 50 and < 150.
55+
/// ```
3256
int nextInt(int max);
3357

3458
/// Generates a non-negative random floating point value uniformly distributed
3559
/// in the range from 0.0, inclusive, to 1.0, exclusive.
60+
///
61+
/// Example:
62+
/// ```dart
63+
/// var doubleValue = Random().nextDouble(); // Value is >= 0.0 and < 1.0.
64+
/// doubleValue = Random().nextDouble() * 256; // Value is >= 0.0 and < 256.0.
65+
/// ```
3666
double nextDouble();
3767

3868
/// Generates a random boolean value.
69+
///
70+
/// Example:
71+
/// ```dart
72+
/// var boolValue = Random().nextBool(); // true or false, with equal chance.
73+
/// ```
3974
bool nextBool();
4075
}

0 commit comments

Comments
 (0)