Randy J. Fortier
randy.fortier@uoit.ca
@randy_fortier
pred = pred - (pred * deathRate)
def probabilityOfDoubles(iterations):
numRolls = 0
numDoubles = 0
for i in range(iterations):
die1 = int(np.random.rand() * 6 + 1)
die2 = int(np.random.rand() * 6 + 1)
numRolls = numRolls + 1
if die1 == die2:
numDoubles = numDoubles + 1
return float(numDoubles) / float(numRolls)
Given numAtoms, decayProb, and timeSteps:
For each of timeSteps:
For each of numAtoms:
Determine if this atom will decay (decayProb)
If yes, then decrease numAtoms
def simulateHalfLife(numAtoms, decayProb, timeSteps):
initialAtoms = numAtoms
for t in xrange(timeSteps):
for m in xrange(numAtoms):
if np.random.rand() < decayProb:
numAtoms -= 1
return initialAtoms, numAtoms
def plotDecay(numAtoms, decayProb, timeSteps):
numRemaining = [numAtoms]
for t in range(timeSteps):
count = numAtoms*((1-decayProb)**t)
numRemaining.append(count)
plt.plot(numRemaining)
plt.xlabel("Time Steps")
plt.ylabel("Atoms")
plt.grid(True)
plt.show()
\begin{equation}\label{01} \begin{split} \frac{area_{circle}}{area_{square}} &= \frac{π\times r^2}{w^2} = \frac{π\times1^2}{2^2} = \frac{π}{4} \end{split} \end{equation}
\begin{equation}\label{02} \begin{split} prob_{square} &= 1.0\\ prob_{circle} &= \frac{π}{4}\\ π &\simeq 4\times\frac{prob_{circle}}{prob_{square}}\\ \end{split} \end{equation}
Inside | Total |
---|---|
156 | 200 |
\begin{equation}\label{03} \begin{split} π_{estimated} &= 4\times\frac{156}{200} = 3.12\\ \end{split} \end{equation}
import math
def estimatePi(iterations):
numInsideCircle = 0
for i in range(iterations):
if dropObject():
numInsideCircle += 1
return 4.0 * float(numInsideCircle) / float(iterations)
def dropObject():
x = np.random.random()
y = np.random.random()
if math.sqrt(x**2 + y**2) < 1.0:
return True
else:
return False