-1

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

1

2 Answers 2

5

You may found an answer here. Try

np.random.choice(a=[False, True], size=(N,))
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.