jumpman8947
jumpman8947

Reputation: 581

python numpy probability output

using python 2.7 and numpy i want to be able to print test results based on a probablity percentage. based on 10 events i want to print out true if it passed and false if it failed. Below is pseudo code

import numpy as np
probability = .33

i'm struggling how to implement using the probablity variable in determining if a test has passed. So in this case the probablity of a test passing 'True' is 33 percent. The probablity does not change for each iteration. its always going to be .33 percent

Ideally it should return something like this

false
false
fasle
true
false
true
fasle
fasle
true
true. 

Upvotes: 0

Views: 148

Answers (1)

Matt Messersmith
Matt Messersmith

Reputation: 13767

You could use the built-in random and generate a number between 0 and 1 (uniform distribution, so all numbers between 0 and 1 are "equally likely"). Then test if that number is less than your desired probability:

import random
def uniform_trials(probability, num_trials):
    for _ in range(num_trials):
        print(probability < random.uniform(0, 1))

Then just call uniform_trials(.33, 10) for your desired example (or any other probability and num_trials you'd like to output).

Upvotes: 1

Related Questions