Get a random boolean in python?

前端 未结 8 1107
无人共我
无人共我 2020-12-04 06:00

I am looking for the best way (fast and elegant) to get a random boolean in python (flip a coin).

For the moment I am using random.randint(0, 1) or

8条回答
  •  没有蜡笔的小新
    2020-12-04 06:44

    A new take on this question would involve the use of Faker which you can install easily with pip.

    from faker import Factory
    
    #----------------------------------------------------------------------
    def create_values(fake):
        """"""
        print fake.boolean(chance_of_getting_true=50) # True
        print fake.random_int(min=0, max=1) # 1
    
    if __name__ == "__main__":
        fake = Factory.create()
        create_values(fake)
    

提交回复
热议问题