-2

Basically, I want to generate a boolean array with given length but the content is randomly given.

feedMe
  • 3,431
  • 2
  • 36
  • 61
Loading Zone
  • 177
  • 2
  • 12
  • 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 Answers2

3

You may found an answer here. Try

np.random.choice(a=[False, True], size=(N,))
Froilan
  • 68
  • 6
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