Feb-20-2018, 02:15 AM
Hi all
I have made a simple script for learning purposes more than for any practical purposes. Since I'm still new to Python I'd like some feedback.
I would guess there is a function somewhere to look for streaks of the same value in lists, but I wanted to avoid that and make something myself. But if you spot something that's either way too long for what it does or is not very 'pythonian' I'd like to hear about it and see som suggestions.
Basically my script generates 100 0/1's (coinflips you could say) and then looks at how how the streaks are of either value. Other than for learning python it's quite interesting that the streaks are much more common and longer than what people guess when asked.
I have made a simple script for learning purposes more than for any practical purposes. Since I'm still new to Python I'd like some feedback.
I would guess there is a function somewhere to look for streaks of the same value in lists, but I wanted to avoid that and make something myself. But if you spot something that's either way too long for what it does or is not very 'pythonian' I'd like to hear about it and see som suggestions.
Basically my script generates 100 0/1's (coinflips you could say) and then looks at how how the streaks are of either value. Other than for learning python it's quite interesting that the streaks are much more common and longer than what people guess when asked.
import random #Description #A simple script to look at randomness and variance(binomial) #1 Generate x amount of numbers(here 100) randomly and store in 'dblist' #2 prepare lists #3 second for loop fills streaks0/streaks1 with length of streaks #4 maxstreaks0/1 lists store max streak values for each 100 numbers #5 main function is run repeatedly - currently 100 times(10k numbers total) # dblist = [] #stores the randomly generated 1/0 shorttermlist0 = 0 #first storage of the streaks length shorttermlist1 = 0 streaks0 = [] #used in the for loop streaks1 = [] #used in the for loop maxstreaks0 = [] #data for each of the samplings(100 numbers each) maxstreaks1 = [] #data for each of the samplings(100 numbers each) def random100(): global dblist global shorttermlist0 global shorttermlist1 global streaks0 global streaks1 global maxstreaks0 global maxstreaks1 for i in range (0,100): currentnum = random.randint(0,1) #print(currentnum) dblist.append(currentnum) #print (dblist) for i in range (0,100): if dblist[i] == 1: shorttermlist1 += 1 streaks0.append(shorttermlist0) shorttermlist0 = 0 else: shorttermlist0 += 1 streaks1.append(shorttermlist1) shorttermlist1 = 0 maxstreaks0.append(max(streaks0)) maxstreaks1.append(max(streaks1)) streaks0 = [] streaks1= [] dblist = [] for _ in range(100): random100() #print(list1) #print(list0) print('overallist0 - max values for each sample of 100: ', sorted(maxstreaks0,reverse=True)) print('overallist1 - max values for each sample of 100: ', sorted(maxstreaks1,reverse=True)) '''#known limitations streak ongoing at 100th number isn't registered '''