Basically, I want to generate a boolean array with given length but the content is randomly given.
Asked
Active
Viewed 2,780 times
-2
-
Possible duplicate of [Create large random boolean matrix with numpy](https://stackoverflow.com/questions/43528637/create-large-random-boolean-matrix-with-numpy) – Kristianmitk Sep 18 '19 at 17:09
2 Answers
1
A faster implementation would be
from numpy.random import default_rng
size = 100_000
rng = default_rng()
rng.integers(0, 1, size, endpoint=True, dtype=bool)
On my machine
%timeit rng.integers(0, 1, size, endpoint=True, dtype=bool)
124 µs ± 595 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit rng.choice(a=[False, True], size=size)
937 µs ± 3.93 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Note that I used the new random generator, instructions here.

Daniele
- 553
- 2
- 12