if '-Ntoss' in sys.argv: p = sys.argv.index('-Ntoss') Nt = int(sys.argv[p+1]) if Nt > 0: Ntoss = Nt if '-Nexp' in sys.argv: p = sys.argv.index('-Nexp') Ne = int(sys.argv[p+1]) if Ne > 0: Nexp = Ne if '-output' in sys.argv: p = sys.argv.index('-output') OutputFileName = sys.argv[p+1] doOutputFile = True # class instance of our Random class using seed random = Random(seed) if doOutputFile: outfile = open(OutputFileName, 'w') for e in range(0,Nexp): for t in range(0,Ntoss): outfile.write(str(random.Bernoulli(prob))+" ") outfile.write(" \n") outfile.close() else: for e in range(0,Nexp): for t in range(0,Ntoss): print(random.Bernoulli(prob), end=' ') print(" ")
#! /usr/bin/env python # imports of external packages to use in our code import sys import numpy as np import matplotlib.pyplot as plt import math #import random from Random2 import Random random = Random() #loop Rayleigh through an empty array to create distribution n=100000 a = [] for x in range(0,n): a.append(random.Rayleigh()) #plot array n, bins, patches = plt.hist(a, 50, density=True, facecolor='g', alpha=0.75) plt.xlabel('x', fontsize = 16, color = "blue") plt.ylabel('Probability', fontsize = 16, color = "red") plt.title('Rayleigh distribution') #plt.legend(['cool green bars'], loc = 'upper left') plt.grid(True) plt.show()
p = sys.argv.index('-Nmeas') Nt = int(sys.argv[p + 1]) if Nt > 0: Nmeas = Nt if '-Nexp' in sys.argv: p = sys.argv.index('-Nexp') Ne = int(sys.argv[p + 1]) if Ne > 0: Nexp = Ne if '-output' in sys.argv: p = sys.argv.index('-output') OutputFileName = sys.argv[p + 1] doOutputFile = True # class instance of our Random class using seed random = Random(seed) if doOutputFile: outfile = open(OutputFileName, 'w') outfile.write(str(rate) + " \n") for e in range(0, Nexp): for t in range(0, Nmeas): outfile.write(str(random.Exponential(rate)) + " ") outfile.write(" \n") outfile.close() else: print(rate) for e in range(0, Nexp): for t in range(0, Nmeas): print(random.Exponential(rate), end=' ') print(" ")
import numpy as np import sys import matplotlib.pyplot as plt import math sys.path.append(".") from Random2 import Random #global variables bin_width = 0. Xmin = 0. Xmax = 1. random = Random() # Rayleigh distribution # Note: multiply by bin width to match histogram def Rayleigh(x): return ((x / (0.1**2)) * np.exp(-x * x / (2 * (0.1**2)))) def PlotRayleigh(x, bin_width): return bin_width * Rayleigh(x) # Uniform (flat) distribution # Note: multiply by bin width to match histogram def Flat(x): return 6.1
def __init__(self, seed = 5555): self.m_random = Random(seed)
class MySort: """A crappy sorting class""" # initialization method for Random class def __init__(self, seed = 5555): self.m_random = Random(seed) # sorts array using bubble sort def BubbleSort(self, array): n = len(array) for i in range(n): # Create a flag that will allow the function to # terminate early if there's nothing left to sort already_sorted = True # Start looking at each item of the list one by one, # comparing it with its adjacent value. With each # iteration, the portion of the array that you look at # shrinks because the remaining items have already been # sorted. for j in range(n - i - 1): if array[j] > array[j + 1]: # If the item you're looking at is greater than its # adjacent value, then swap them array[j], array[j + 1] = array[j + 1], array[j] # Since you had to swap two elements, # set the `already_sorted` flag to `False` so the # algorithm doesn't finish prematurely already_sorted = False # If there were no swaps during the last iteration, # the array is already sorted, and you can terminate if already_sorted: break return array # sorts array using insertion sort def InsertionSort(self, array): # Loop from the second element of the array until # the last element for i in range(1, len(array)): # This is the element we want to position in its # correct place key_item = array[i] # Initialize the variable that will be used to # find the correct position of the element referenced # by `key_item` j = i - 1 # Run through the list of items (the left # portion of the array) and find the correct position # of the element referenced by `key_item`. Do this only # if `key_item` is smaller than its adjacent values. while j >= 0 and array[j] > key_item: # Shift the value one position to the left # and reposition j to point to the next element # (from right to left) array[j + 1] = array[j] j -= 1 # When you finish shifting the elements, you can position # `key_item` in its correct location array[j + 1] = key_item return array # sorts array using quicksort def QuickSort(self, array): # If the input array contains fewer than two elements, # then return it as the result of the function if len(array) < 2: return array low, same, high = [], [], [] # Select your `pivot` element randomly pivot = array[int(self.m_random.rand()*len(array))] for item in array: # Elements that are smaller than the `pivot` go to # the `low` list. Elements that are larger than # `pivot` go to the `high` list. Elements that are # equal to `pivot` go to the `same` list. if item < pivot: low.append(item) elif item == pivot: same.append(item) elif item > pivot: high.append(item) # The final result combines the sorted `low` list # with the `same` list and the sorted `high` list return self.QuickSort(low) + same + self.QuickSort(high) # sorts array using default Python sort def DefaultSort(self, array): array.sort() return array
if '-Nexp' in sys.argv: p = sys.argv.index('-Nexp') Ne = int(sys.argv[p + 1]) if Ne > 0: Nexp = Ne if '-output' in sys.argv: p = sys.argv.index('-output') OutputFileName = sys.argv[p + 1] doOutputFile = True #extract individual probabilities from list; may have to automate if you choose to work with dice w/o 6 sides prob1, prob2, prob3, prob4, prob5, prob6 = prob_list[0], prob_list[ 1], prob_list[2], prob_list[3], prob_list[4], prob_list[5] # class instance of our Random class using seed random = Random(seed) if doOutputFile: outfile = open(OutputFileName, 'w') for e in range(0, Nexp): for t in range(0, Nroll): outfile.write( str( random.Diceroll(prob1, prob2, prob3, prob4, prob5, prob6)) + " ") outfile.write(" \n") outfile.close() else: for e in range(0, Nexp): for t in range(0, Nroll): print(random.Diceroll(prob1, prob2, prob3, prob4, prob5,