# Untitled

## 964 days ago by pub

Wikipages: http://sagemath.wikispaces.com/histograms  (Scroll down here to see histogram.)

Problem: Suppose of a population has a characteristic A where 0≤p≤ 1. How can we generate and manipulate a sample set for this characteristic?

Create a function random_between(j,k) that returns a (pseudo) random integer between the integers j and k.

Create a function binomial_trial(p) that returns 1 (success=has characteristic A) or 0 (failure=does not have characteristic A).

Create a function count_if(list1,p) that returns the number of elements of list1 that have value ≤ p*100.

def random_between(j,k): a=int(random()*(k-j+1))+j return a
def binomial_trial(p): a=random_between(1,100) if a<=p*100: b=1 else: b=0 return b
def count_if(list1,p): c=len([x for x in list1 if x<=p*100]) return c
#generate a random number between 1 and 100 and display it. r=random_between(1,100) print r
 13 13
#run a binomial trial for some p p=0.3 binomial_trial(p)
 0 0
#create a list of n binomial trials for p n=10 bin_exp=[binomial_trial(p) for j in range(n)] print bin_exp
 [0, 0, 0, 0, 0, 0, 0, 1, 0, 1] [0, 0, 0, 0, 0, 0, 0, 1, 0, 1]
#create a list of n binomial trials for p and find the mean (should be closer to p the bigger the n). n=100 bin_exp=[binomial_trial(p) for j in range(n)] mu=mean(bin_exp) print float(mu)
 0.25 0.25