Get a random boolean in python?

前端 未结 8 1069
无人共我
无人共我 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 07:02

    I like

     np.random.rand() > .5
    
    0 讨论(0)
  • 2020-12-04 07:03

    You could use the Faker library, it's mainly used for testing, but is capable of providing a variety of fake data.

    Install: https://pypi.org/project/Faker/

    >>> from faker import Faker
    >>> fake = Faker()
    >>> fake.pybool()
    True
    
    0 讨论(0)
提交回复
热议问题