jeudi 8 août 2019

How to consistently determine how many digits my round.random.uniform has?

This code:

import random

while True:
    thing = str(round(random.uniform(1,3), 3))
    print(thing)

Is not consistently generating numbers with 3 decimals, giving me sometimes numbers with only 2 decimals.

Output example:

1.575
2.035
2.864
2.118
1.069
2.36
1.092
1.948
2.54
2.224
2.318
1.713
1.819
2.717
1.171
2.918
3.0
1.23
2.487

I'm able to fix this creating a routine to check the length of my number, and then I append a string of another random generated integer. Like

if len(thing) < 5:
    thing = thing + str(2)

I would like to know, however, how I can set parameters to generate random number with fixed lengths at all times.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire