3

I want to fill randomly a numpy array with arbitrary size with zeroes and ones. On each entry there should be a one with probability p.

    for x in range(l):
        rn = numpy.random.uniform(0, 1)
        if rn <= p:
            arr[x] = 1.0
        else:
            arr[x] = 0.0

Do you know a faster way to do this?

1 Answer 1

8

You want to use the Binomial distribution.

arr = numpy.random.binomial(1, p, count)
Sign up to request clarification or add additional context in comments.

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.