Get a random boolean in python?

前端 未结 8 1102
无人共我
无人共我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 06:48

    If you want to generate a number of random booleans you could use numpy's random module. From the documentation

    np.random.randint(2, size=10)
    

    will return 10 random uniform integers in the open interval [0,2). The size keyword specifies the number of values to generate.

提交回复
热议问题