Python Forum
A simple script - looking for feedback
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A simple script - looking for feedback
#1
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.

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

'''
Reply


Messages In This Thread
A simple script - looking for feedback - by glidecode - Feb-20-2018, 02:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Python script, path not defined dubinaone 3 2,746 Nov-06-2021, 07:36 PM
Last Post: snippsat
  Need help creating a simple script Nonameface 12 4,706 Jul-14-2020, 02:10 PM
Last Post: BitPythoner
  Simple text to binary python script gmills13 2 2,857 Feb-04-2020, 08:44 PM
Last Post: snippsat
  Made a simple script for android (really simple) but it is not running anddontyoucomebacknomore 2 2,403 Mar-06-2019, 12:19 AM
Last Post: anddontyoucomebacknomore
  Feedback and help tomX 13 5,642 Dec-31-2018, 11:00 PM
Last Post: Larz60+
  Simple script that seems to misbehave? Nwb 1 2,384 Jun-10-2018, 05:30 AM
Last Post: Nwb
  First time with Python.. need help with simple script shakir_abdul_ahad 7 5,585 May-06-2018, 09:28 AM
Last Post: killerrex
  help with a simple script juanb007 4 3,688 May-01-2018, 08:06 PM
Last Post: ThiefOfTime
  Simple script writted by a dumb dude, myself mm14ag 2 2,795 Apr-28-2018, 11:48 AM
Last Post: mm14ag
  Need help with a simple AHK script Stabu 0 2,117 Feb-24-2018, 08:27 PM
Last Post: Stabu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020