+ 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

30th May 2017, 8:47 AM
SC Lee
SC Lee - avatar
3 ответов
+ 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.
30th May 2017, 9:23 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
hi.. I like to learn coding from u? any media to keep in touch? I'm 17
7th Jun 2017, 6:40 PM
Zack zakariya
Zack zakariya - avatar
0
Thanks. It works when using Random class together with the rand method. Random.rand(5.0)
30th May 2017, 11:11 AM
SC Lee
SC Lee - avatar