alpha=0.75) # plot formating options plt.xlabel('Dice faces') plt.ylabel('Probability', fontweight="bold", fontsize="17") plt.title('Categorical Distribution', fontweight="bold", fontsize="17") plt.grid(True, color='r') # save and show figure plt.savefig("Dice.png") #plt.show() # an array of rayleigh numbers using our Random class myx = [] for i in range(0, 10000): myx.append(random_number.Rayleigh()) # create histogram of our data plt.figure() plt.hist(myx, 70, density=True, facecolor='y', alpha=0.75) # plot formating options plt.xlabel('x') plt.ylabel('Probability') plt.title("Rayleigh Distribution", fontweight="bold") plt.grid(True) # show figure (program only ends once closed plt.show() plt.savefig("Rayleigh.png")
# main function for this Python code if __name__ == "__main__": # Set up parser to handle command line arguments # Run as 'python PHSX815_HW2_KurtH.py -h' to see all available commands parser = argparse.ArgumentParser() parser.add_argument("--seed", "-s", help="Seed number to use") args = parser.parse_args() # default seed seed = 5555 if args.seed: print("Set seed to %s" % args.seed) seed = args.seed # set random seed for numpy np.random.seed(seed) # class instance of our Random class using seed random = Random(seed) # create some random data N = 10000 # an array of random numbers using our Random class myx = np.array([]) for i in range(0, N): myx = np.append(myx, random.Rayleigh()) make_plot(myx) plt.show()
if args.Nsides: print("Number sides per die: %s" % args.Nsides) Nsides = np.uint64(args.Nsides) Nrolls = 100 if args.Nrolls: print("Number of rolls per experiment: %s" % args.Nrolls) Nrolls = np.uint64(args.Nrolls) Nexp = 1000 if args.Nexp: print("Number of experiments: %s" % args.Nexp) Nexp = np.uint64(args.Nexp) # Create weights by pulling from a rayleigh distribution weights = np.array([random.Rayleigh() for i in range(Nsides)]) # The weigths need to sum to one, so normalize them weights /= weights.sum() print(f'Weights: {weights}') rolls_test = np.zeros((Nexp, Nrolls)) rolls_fair = np.zeros((Nexp, Nrolls)) for i in range(Nexp): for j in range(Nrolls): rolls_test[i, j] = random.roll_die(Nsides=Nsides, weights=weights) rolls_fair[i, j] = random.roll_die(Nsides=Nsides) # Save the normalized results np.savetxt('data/dice_rolls.txt', rolls_test, delimiter=',', fmt="%d") np.savetxt('data/fair_dice_rolls.txt', rolls_fair, delimiter=',', fmt="%d")