0

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. 

1 Answer 1

1

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).

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.