+ 3
Why returned value is not a random floating point number?
rand(max)->number Refer to Ruby documentation, when max is a Float, rand returns a random floating point number between 0.0 and max, including 0.0 and excluding max. However, running the code rand(5.0) does not return float number. Can anyone enlighten me? https://code.sololearn.com/cZbSQTLAYZWy/?ref=app
3 Respostas
+ 7
If you use an instance of the Random class to call rand(), it will return a float in your case. E.g.
prng = Random.new
prng.rand(5.0)
#returns random floating point value.
What I can think of is that the original rand() function is not overloaded for float values when a range is not specified, i.e. the function only accepts integer values when only a single number is provided.
+ 1
hi.. I like to learn coding from u? any media to keep in touch? I'm 17
0
Thanks. It works when using Random class together with the rand method.
Random.rand(5.0)