Generate random number between 0.1 and 1.0. Python

后端 未结 10 1890
南方客
南方客 2020-12-29 05:23

I\'m trying to generate a random number between 0.1 and 1.0. We can\'t use rand.randint because it returns integers. We have also tried random.uniform(0.1

10条回答
  •  悲哀的现实
    2020-12-29 05:35

    Are you unable to use random.random()? This gives a number between 0.0 and 1.0, though you could easily set up a way to get around this.

    import random
    def randomForMe():
        number = random.random()
        number = round(number, 1)
        if (number == 0):
            number = 0.1
    

    This code would give you a number that is between 0.1 and 1.0, inclusive (0.1 and 1.0 are both possible solutions). Hope this helps.

    *I assumed you only wanted numbers to the tenths place. If you want it different, where I typed round(number, 1) change 1 to 2 for hundredths, 3 for thousandths, and so on.

提交回复
热议问题