0

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).

3 Answers 3

4

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]
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.