 wwright999
6/14/2017 - 9:58 PM

## Random Integer Between 0-1

Random Integer Between 0-1

``````from random import expovariate, gauss
from statistics import mean, median, stdev

average_arrival_interval = 5.6
average_service_time = 5.0
stdev_service_time = 0.5

num_waiting = 0
arrivals = []
starts = []
arrival = service_end = 0.0
for i in range(20000):
if arrival <= service_end:
num_waiting += 1
arrival += expovariate(1.0 / average_arrival_interval)
arrivals.append(arrival)
else:
num_waiting -= 1
service_start = service_end if num_waiting else arrival
service_time = gauss(average_service_time, stdev_service_time)
service_end = service_start + service_time
starts.append(service_start)

waits = [start - arrival for arrival, start in zip(arrivals, starts)]
print(f'Mean wait: {mean(waits):.1f}.  Stdev wait: {stdev(waits):.1f}.')
print(f'Median wait: {median(waits):.1f}.  Max wait: {max(waits):.1f}.')``````
``````# CREAT A 4X4 array of random numbers from normal distribution
import numpy as np
samples = np.random.normal(size=(4,4))``````
``````# Prints 10 Random Integers
import random

for i in range(10):
x = random.random()
print(x)

#Excerpt From: Charles R Severance. “Python for Everybody.” iBooks. ``````
``````# SIMULATES A RANDOM WALK
import random
position = 0
walk = [position]
steps = 1000
for i in range(steps):
step = 1 if random.randint(0,1) else -1
position += step
walk.append(position)

# RANDOM COIN FLIPS
nsteps = 1000
draws = np.random.randint(0,2,size=nsteps)
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum
walk.min()
walk.max()

# Calculate How Many Steps to Reach 10
(np.abs(walk) >= 10).argmax() #

``````
``````# FUNCTION FOR CREATING MULTIPLE RANDOM WALKS
nwalks = 5000
nsteps =1000
steps = np.where(draws > 0, 1, -1)
walk = steps.cumsum(1)
walks
walks.max()
walks.min()

# MINIMUM CROSSING TIME TO 30 OR -30
hits30 = (np.abs(walks) >= 30).any(1)
hits30
hits30.sum() # Number that hit 30 or -30

#CROSSING TIME MEAN
crossing_times = (np.abs(walks[hits30]) >= 30).argmax(1)
crossing_times.mean()

# CREATE A DISTRIBUTION WITH A MEAN AND STDEV
steps = np.random.normal(loc=0, scale=.25,size=(nwalks, nsteps))``````