Feb-20-2018, 11:18 PM
Thanks a lot for the feedback!
My changes since previous version of the script:
1) completely removed the global statements and the initializing of variables at the beginning.
2) changed from using indices of lists to using the elements directly(I guess that is it)
3) separated the script into 3 functions
Here is what it looks like now:
My changes since previous version of the script:
1) completely removed the global statements and the initializing of variables at the beginning.
2) changed from using indices of lists to using the elements directly(I guess that is it)
3) separated the script into 3 functions
Here is what it looks like now:
import random #Description #A simple script to look at randomness and variance(binomial) #1 The function 'numbergenerator' generates x amount of numbers # (here 100) randomly and stores them in 'dblist' #2 'streakcounter' returns longest streak in each 100 number sample #3 'maxcounter' creates two lists with streaks from each 100 number sample def numbergenerator(): storagelist = [] for i in range(0,100): currentnum = random.randint(0,1) storagelist.append(currentnum) return(storagelist) def streakcounter(): dblist = numbergenerator() streaks0 = [] # used in the for loop streaks1 = [] # used in the for loop shorttermlist0 = 0 shorttermlist1 = 0 for i in dblist: if i == 1: shorttermlist1 += 1 streaks0.append(shorttermlist0) shorttermlist0 = 0 else: shorttermlist0 += 1 streaks1.append(shorttermlist1) shorttermlist1 = 0 return(max(streaks0),max(streaks1)) def maxcounter(times): streakcounterlist0 = [] streakcounterlist1 = [] for _ in range(times): zeros, ones = streakcounter() streakcounterlist0.append(zeros) streakcounterlist1.append(ones) print('max streaks of 0s from %i samples' %(times),streakcounterlist0) print('max streaks of 1s from %i samples' %(times), streakcounterlist1) maxcounter(100)