Reputation: 264
I need to generate a random number between 0 and 7. but it must include decimals up to around 8 places. For example: 3.54367334 or 6.3464357. I haven't found anything in the random module that would give me something like that. Everything is with integers only, or from 0 to 1 (which is random.random).
Upvotes: 0
Views: 320
Reputation: 1
You can use this code:
random.random() * 7
As random.random
gives a number between 0 and 1.0, * 7
will give between (0, 7.0)
Upvotes: 0
Reputation: 129001
As you noticed, random.random
returns a value in the range [0.0, 1.0). If you multiply its return value by 7, then the result will be in the range [0.0, 7.0).
There is also a function, random.uniform
, which, given the range, will do this for you.
Upvotes: 4
Reputation: 353099
While you could rescale random.random
, why not use random.uniform instead?
>>> import random
>>> [random.uniform(0, 7) for i in range(4)]
[3.36879678022553, 1.3262017420571945, 2.961415926838038, 6.409486371437773]
Upvotes: 4