After running this program several times i noticed that my y value is somewhere between 60-80 every single time.
I thought because 70^2 is 4900 that i would end up with my y value ending up around 1 each run through, but actually its about a 1 in 70 chance of the dice equaling each other.
So why is it that rolling 2 70 sided dice and having the results equal each other is not a 1/4900 chance, but rather a 1/70 chance? Heres the program...
x=0
y=0
while x < 4900
random = rand(70)
random2 = rand(70)
puts " "
puts random
puts random2
if random == random2
puts "the numbers matched"
y+=1
end
x+=1
if x == 4900
puts " "
puts y
end
end
There are 4900 possible outcomes (1,1), (1,2), (1,3) .. ,(70, 70)
There are 70 outcomes that are suitable for your condition - (1,1), (2,2) .. (70,70)
So, the probability is needed_outcomes/all_outcomes = 70/4900 = 1/70 ~= 0.0142858 In test program number of tests is not connected to number of outcomes. Larger number of tests tends to show more accurate results (through in this case we don't program at all, but there is ruby tag in the question).
So, we can try this:
x=0
total_matches = 0.0
N = 1000000
while x < N
random = rand(1..70)
random2 = rand(1..70)
total_matches += 1 if random == random2
x += 1
end
puts total_matches/N
It gives something around 0.0142.