I created a code that was supposed to determine how well my system of guessing is. The scenario is that there is n amount of tanks. Each tank is uniquely numbered 1 to n. Seven of these tanks are captured so we only know their serial number (This is done by getting a random number from 1 to n for each of the seven tanks). I am supposed to predict n based on those serial numbers. My method (the one I have to code) was to first find the max number in the list and the number three standard deviations above the mean of the list. Then, I get my own random numbers from 1 to n where n ranges from the max number and the three standard deviations number. During this time, I calculate the standard deviation for each of the seven serial numbers generated and after all of the standard deviations are calculated, I find which one is closest to the standard deviation of the original list. Whichever one is the closest to the standard deviation would constitute as my guess for n. I repeat this process as many times as necessary (the more times the more accurate results) to generate a list of guesses. My final guess then would be the mean of that list. My code is below:
import random import statistics chosenNum = 429 #n is given only to check how well our process works numRuns = 100 #number of guesses - my teacher wants at least 100 numCorrect = 0 numGuessN = [] guesses = [] percentErrors = [] for x in range(numRuns): randNum = [random.randint(1,chosenNum + 1),random.randint(1,chosenNum + 1),random.randint(1,chosenNum + 1),random.randint(1,chosenNum + 1),random.randint(1,chosenNum + 1),random.randint(1,chosenNum + 1),random.randint(1,chosenNum + 1)] #generates the seven serial numbers. NumSTD = statistics.stdev(randNum) #standard deviation maxNumSTD = statistics.mean(randNum)+(3*(statistics.stdev(randNum))) #three standard deviations above the mean maxNum = max(randNum) #max number in list #print (NumSTD) #print (maxNumSTD) #print (maxNum) #print (randNum) for x in range(200): #the greater the range, the more accurate the results STDarray = [] if (maxNum - maxNumSTD < 0): for y in range(maxNum,int(maxNumSTD)): #n is max number to max number from Standard Deviation randNumZwei = [random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y)] #my simulated serial numbers testNumSTD = statistics.stdev(randNumZwei) STDarray.append(testNumSTD) else: for y in range(int(maxNumSTD),maxNum): randNumZwei = [random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y),random.randint(1,y)] testNumSTD = statistics.stdev(randNumZwei) STDarray.append(testNumSTD) for z in range((len(STDarray) - 1)): if((min(STDarray, key=lambda x:abs(x-NumSTD))) == STDarray[z]): #find closest number to original standard deviation numGuessed = z + maxNum numGuessN.append(numGuessed) #make a list of all the guessed numbers guess = int(statistics.mean(numGuessN)) #the final guess is simply mean of all the other guesses generated #print(guess) guesses.append(guess) #number of final guesses should be the same as number of trials print ("Your guesses are: " + str(guesses)) for x in range(len(guesses) - 1): percentError = (abs(guesses[x] - (chosenNum))/float(chosenNum)) * 100 percentErrors.append(percentError) if(guesses[x] == chosenNum): numCorrect = numCorrect + 1 else: closestNumber = min(guesses, key=lambda x:abs(x-chosenNum)) averagePercentError = statistics.mean(percentErrors) print ("The average Percent Error is: " + str(averagePercentError) + "%") if (numCorrect > 0): print ("You got the correct number " + str(numCorrect/float(len(guesses)))) else: print ("Your closest number was: " + str(closestNumber))
This code works but takes way too long to give me my result. The whole point of this is to calculate accuracy but not take way too long. How can I make this code more efficient to run faster.