Skip to content

Instantly share code, notes, and snippets.

@ashwin
Created May 3, 2012 10:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashwin/2585006 to your computer and use it in GitHub Desktop.
Save ashwin/2585006 to your computer and use it in GitHub Desktop.
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
Copy link

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