Hey Guys,

I'm trying to learn some python and as part of this I am tring to generate 100 weighted random numbers and then check that the output that I get is within the expected range.

So far I have a function that will generate the weighted random number and return a list of all returned values and the count of all the returned values. The code I have so far is below along with the returned output.

code: (unit_test1 is the function I intend to analyse the output in)

example outputCode:import random import bisect from collections import Counter class RandomGen(object): # Values that may be returned by next_num() _random_nums = [-2, -1, 0, 1, 2] # Probability of the occurence of random_nums _probabilities = [0.01, 0.2, 0.58, 0.2, 0.01] def next_num(self): """ Returns one of the randomNums. When this method is called multiple times over a long period, it should return the numbers roughly with the initialized probabilities. """ totals = [] running_total = 0 # Create a cummulative frequency range. for p in self._probabilities: running_total += p totals.append(running_total) # If not warn the user before continuing. if running_total != 1: print("Warning: Total probabilities to not sum to 1") print("Normalising probabilities. Check your input.") # Generate a randdom number with the range 0 to running_total rand=random.random() * running_total # Find the index to return idx = bisect.bisect_right(totals, rand) return self._random_nums[idx] def unit_test1(self): """ The aim of this test it to check that the observed values are within the expected chi square range """ pass allValues = [] randomNum = RandomGen() numRuns = 100 # Run the program 100 times and output the sums of each value. for i in range(numRuns): value = randomNum.next_num() allValues.append(value) # Display the results to the user. print Counter(allValues) print allValues

The next bit is where I am stuck. In order to calculate the chi-square I think I need my output data to be in the format:Code:Counter({0: 64, -1: 22, 1: 14}) [-1, 0, 0, -1, 0, 0, 0, 1, -1, 0, 0, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -1, 0, 0, 0, -1, 0, -1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, -1, -1, 0, 0, 0, -1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, -1, 0, -1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, -1, -1, -1, -1, 0, 0, 0, -1, 0, -1, 1, 1, -1, 0, 0, 1]

How do I go from the output I have to this format?Code:observed_nums = [ 0, 22, 64, 14, 0 ]

Also from searching on line it looks like the for loop to calculate my random values may not be the most efficient way to do this. I have used this method as it is all I know how to do. If you have any suggestions on how to improve the code in general it would be appreciated.

Thanks

Tweet This+ 1 thisPost To Linkedin