Example #1
0
from math import cos, atan

N =      10000                      # number of replications */
HALF_PI =(2.0 * atan(1.0))          # 1.5707963...           */
R   =    1.0                        # length of the needle   */

def Uniform(a,b):  
# --------------------------------------------
# * generate a Uniform random variate, use a < b 
# * --------------------------------------------
# */
  return (a + (b - a) * random())  

################################Main Program#############################

putSeed(-1)                   # any negative integer will do      */
seed = getSeed()              # trap the value of the intial seed */
crosses = 0                   # tracks number of crosses

for i in range(0,N):                
  u     = random() #get first endpoint                                  
  theta = Uniform(-HALF_PI, HALF_PI) #get Angle
  v     = u + R * cos(theta) #get second endpoint
  if (v > 1.0):
    crosses += 1 #increase number of crosses


p = float(crosses / N)                # estimate the probability */

print("\nbased on {0:1d} replications and a needle of length {1:5.2f}".format(N, R))
print("with an initial seed of {0:1d}".format(seed))
Example #2
0
  delay = 0.0  #delay times
  wait = 0.0 #wait times
  service = 0.0 #service times
  interarrival = -1.0 #interarrival times

#################################Main Program############################

index = 0                        # job index            */
arrival = START                    # arrival time         */
delay = -1                               # delay in queue       */
service = -1                             # service time         */
wait = -1                                 # delay + service      */
departure = START                    # departure time       */
sum = sumOf()

putSeed(123456789)

while (index < LAST):
  index += 1
  arrival      = GetArrival()
  if (arrival < departure):
    delay      = departure - arrival         # delay in queue    */
  else:
    delay      = 0.0                         # no delay          */
  service      = GetService()
  wait         = delay + service
  departure    = arrival + wait              # time of departure */
  sum.delay   += delay
  sum.wait    += wait
  sum.service += service
#EndWhile 
Example #3
0
def Equilikely(a,b):        
# # ------------------------------------------------
# * generate an Equilikely random variate, use a < b 
# * ------------------------------------------------
# */
  return (a + int((b - a + 1) * random()))




# i                               # replication index      */
# x                               # sum of three dice      */
count=[0 for i in range(0,19)]    # histogram              */
p=[0.0 for i in range(0,19)]      # probability estimates  */

putSeed(0)

for i in range(0,N): 
  x = Equilikely(1, 6) + Equilikely(1, 6) + Equilikely(1, 6)
  count[x] += 1

for x in range(3,19):             # estimate probabilities */
  p[x] = float(count[x]) / N

print("\nbased on {0:d} replications the estimated probabilities are:\n".format(N))
for x in range(3,19):
  print("p[{0:2d}] = {1:5.3f}".format(x, p[x]))


# C output:
# Enter a positive integer seed (9 digits or less) >> 123456789
Example #4
0
    test = 0
    condition = True

    while (condition == True):  # test to see if at least */
        test = (a[j] == j)  # one element is in its   */
        j += 1  # 'natural' position      */
        condition = (j != SIZE) and (test == 0)  # - return a 1 if so      */
        # - return a 0 otherwise  */
    if (test == 1):
        return (1)
    else:
        return (0)


###############################Main Program##############################
putSeed(0)
arr = [None for i in range(0, SIZE)]
Initialize(arr)

for i in range(0, N):  # do N Monte Carlo replications */
    Shuffle(arr)
    count += Check(arr)

p = float(N - count) / N  # estimate the probability */

print("\nfor {0:1d} replications and an array of size {1:d}".format(N, SIZE))
print("the estimated probability is {0:5.3f}".format(p))

# c output:
# Enter a positive integer seed (9 digits or less) >> 123456789