Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate random float in Python
import random
# Random float in [0.0, 1.0)
a = random.random()
# Random float in [0, 100]
b = random.uniform( 0, 100 )
@JayRizzo

This comment has been minimized.

Copy link

@JayRizzo JayRizzo commented Oct 11, 2018

I needed 0.01 -> 1.00 so used what you got and modified it a little. This is probably way overkill but wanted to share. =)

# Random float in [0.01, 1.00]
c = "{:00.2f}".format(round(random.uniform(1, 100) * .01, 2))
'0.07'
'0.01'
'0.51'
'0.07'
'0.86'
'0.71'
'0.77'
'0.49'
'0.70'
'0.44'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.