Project

General

Profile

Actions

Bug #16180

closed

Random.rand(max) ignores / is broken when max ceiling value is a float

Added by BillyRuffian (Nigel Brookes-Thomas) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
[ruby-core:95081]

Description

ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
ruby 2.7.0preview1 (2019-05-31 trunk c55db6aa271df4a689dc8eb0039c929bf6ed43ff) [x86_64-darwin18]

Calling rand with a float of > 1 always return 0.

Calling rand with a float 0 < value > 1 will ignore the ceiling value.

e.g.

rand(1.1)
=> 0
rand(1.1)
=> 0
rand(0.5)
=> 0.9501516156613756
rand(0.5)
=> 0.6864252478379304

Updated by Hanmac (Hans Mackowiak) over 4 years ago

your cases are documented there: https://ruby-doc.org/core-2.6.4/Kernel.html#method-i-rand

When max.abs is greater than or equal to 1, rand returns a pseudo-random integer greater than or equal to 0 and less than max.to_i.abs.

Negative or floating point values for max are allowed, but may give surprising results.

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Status changed from Open to Rejected

If you want random Float values up to max, use Random#rand or Random.rand instead of Kernel#rand.

Actions

Also available in: Atom PDF

Like0
Like0Like0