Skip to content

Instantly share code, notes, and snippets.

@ImSingee
Created May 8, 2018 06:16
Show Gist options
  • Save ImSingee/5d3c4f6c7f1d119b4e37bcaeb7513aba to your computer and use it in GitHub Desktop.
Save ImSingee/5d3c4f6c7f1d119b4e37bcaeb7513aba to your computer and use it in GitHub Desktop.
取 [start, end) 间的随机数,保留 accuracy 位小数
def get_random(start=0, end=1, accuracy=0):
rnd = random.random() # [0, 1)
rnd = (end - start) * rnd + start # [start, end)
if accuracy == 0:
return int(rnd)
else:
return round(rnd, accuracy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment