Skip to content

Commit 01e27ac

Browse files
authored
[DOC] Clarify differences between Kernel.rand and Random.rand when given a Float max
* [DOC] Clarify differences between Kernel.rand and Random.rand * `Kernel.rand` converts the `max` Float argument to Integer and returns Integer values * `Random.rand` preserves the `max` Float argument and returns Float values * Added examples demonstrating the behavioral differences
1 parent de6df75 commit 01e27ac

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

random.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,9 +1552,11 @@ static VALUE rand_random(int argc, VALUE *argv, VALUE obj, rb_random_t *rnd);
15521552
* prng.rand(100) # => 42
15531553
*
15541554
* When +max+ is a Float, +rand+ returns a random floating point number
1555-
* between 0.0 and +max+, including 0.0 and excluding +max+.
1555+
* between 0.0 and +max+, including 0.0 and excluding +max+. Note that it
1556+
* behaves differently from Kernel.rand.
15561557
*
1557-
* prng.rand(1.5) # => 1.4600282860034115
1558+
* prng.rand(1.5) # => 1.4600282860034115
1559+
* rand(1.5) # => 0
15581560
*
15591561
* When +range+ is a Range, +rand+ returns a random number where
15601562
* <code>range.member?(number) == true</code>.
@@ -1692,7 +1694,9 @@ rand_mt_equal(VALUE self, VALUE other)
16921694
* Kernel.srand may be used to ensure that sequences of random numbers are
16931695
* reproducible between different runs of a program.
16941696
*
1695-
* See also Random.rand.
1697+
* Related: Random.rand.
1698+
* rand(100.0) # => 64 (Integer because max.to_i is 100)
1699+
* Random.rand(100.0) # => 30.315320967824523
16961700
*/
16971701

16981702
static VALUE

0 commit comments

Comments
 (0)