If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: High school statistics>Unit 6

Lesson 4: Probability from simulations

# Random number list to run experiment

Using a list of random numbers to simulate multiple trials of an experiment.

## Want to join the conversation?

• I understand that the more experiments we conduct, we get closer to the theoretical average, but how do you find the theoretical average for the question that Sal solved?
• If we already have 𝑛 different prizes, then the probability of getting a prize we don't have from the next box of cereals is
𝑝(𝑛) = 1 − 𝑛∕6 = (6 − 𝑛)∕6

The average number of trials until we get a successful trial is
𝑡(𝑛) = 1∕(𝑝(𝑛)) = 6∕(6 − 𝑛)

So, starting with zero prizes, the average number of trials until we have all six prizes is
𝑡(0) + 𝑡(1) + 𝑡(2) + ... + 𝑡(5) =
= 6∕(6 − 0) + 6∕(6 − 1) + 6∕(6 − 2) + ... + 6∕(6 − 5) =
= 14.7
• What is an intuition behind choosing numbers between 0-9 to run simulation experiment?
• The random numbers generated are going to be 0-9. By using all digits, randomness is ensured. Since all ten digits will be used in the generation, and only those digits, you use 0-9 to run the simulation. I hope that helps!
• Hi Sal, Why don't you look at the invalid numbers? Cause in real life she does not know if the box has a prize or not and she choses a box unknowingly and thus i would say in the first experiment is 12 boxes of of the total number of boxes? Am i right or what point am i missing :-) I mean i can't go to wall mart and return a box because it has no prize...
Dine
• There are only 6 prizes and every box has a prize. The invalid numbers do not represent a box without a prize, they are simply invalid because they are not needed for how the problem is constructed. If the problem had stated there were only 3 prizes, then we would ignore any number greater than 3 in the random digit table, because it would have no meaning in the context of the problem.
• Isn't there any other way to calculate this probability? Do we need to run a random digit table and count 1 to 6 for every experiment and then find the average ? This will be tiresome. Mathematics is to make things easy so how is it helpful?
• For this one, no, because we know a fair 6-sided die can only get us numbers 1-6, and three rolls of the die will get us combinations of numbers from 1-6.
The solution: get a table, and write down every single possible outcome, from 111, 112...to 666. Then, circle the outcomes where you win, divide by the number of total possible outcomes. It turns out to be 50%.
And further in the course you will learn combinations and permutations, which deals with exactly this kind of stuff.
• So is there a rule of how many times we need to run the experiment to get the average number?
• Here's a python code that simulates the experiment

import random

total_count=0

def all_collected(numList)->bool:
result=False
temp=0
for i in range(0,len(collection)):
temp+=collection[i]
if temp==21:
return True

return result

for i in range(0,100000):
collection = [0, 0, 0, 0, 0, 0]
count=0
while not all_collected(collection):

new=random.randint(1,6)
if new not in collection:
collection[new-1]=new

count+=1

total_count+=count
print(i)
average_attempt=round(total_count/100000,2)

print('final:'+str(average_attempt))
• Nice code!
(1 vote)
• How do you know how many experiments to do or what to stop at?
• The more experiments you do, the more accurate your probability will be. So, it really depends on you, so you can balance accuracy and your convenience.
• So using a random sequence simulation is only valid where the samples have an equal likelihood?
• yes, using a random sequence simulation is valid when each sample (in this case, each box) has an equal likelihood of containing any of the prizes. this ensures that the simulation accurately represents the randomness of the process
• I made this in python if you guys want to try it out, in this case, I wanted to find out how many times I roll a die to get all 6 numbers. It's experimental and serves the same purpose.
#The setup:
import random
all_rolls = []

max_items = 9
mi = 1
max_items_list = []

while mi <= max_items:

max_items_list.append(mi)
mi+=1

def dice_function():
results =[]
throws = 20000
dice_sides = max_items_list
i=0
while i <throws:
dice_throw = round(random.randint(1,max_items))
results.append(dice_throw)
if all(elem in results for elem in dice_sides):
break
i+=1

all_rolls.append(len(results))

#The LAY-OUT
print ("IT TOOK " + str(len(results)) + " ITERATIONS TO COMPLETE THE SET:")
print (results)
print (" ")
print ("ALL ROLLS: " + str(all_rolls))
print ("MEAN: " + str((sum(all_rolls))/len(all_rolls)))
print (" ")
print (" ")
print (" ----------------------------------------------------------- ")
print (" ")
print (" ")

x = 0
while x < 1000:
dice_function()
x+=1