Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Generated Random Strings Of User-Defined Length Binary - Python Code Bank


Generated Random Strings Of User-Defined Length Binary
Does What I Says In The Title... and if that doesn't help look at the code!
                import random
import sys

numberArgs = len(sys.argv)
try:
    anyStringInt = sys.argv[1]
    anyStringInt = int(anyStringInt)
except:
    sys.exit("Syntax: reg.py [integer number of randoms to generate]")
if numberArgs != 2: print "Syntax: reg.py [integer number of randoms to generate]"
else:
    numberFlips = int(sys.argv[1])
    numberOnes = 0
    numberTwos = 0
    whileCount = 0
    tempOnes = 0
    tempTwos = 1
    binaryList = []    

    while whileCount < numberFlips:
        randomNum = random.randrange(1,3)
        if randomNum == 1:
            numberOnes = numberOnes + 1
            tempOnesInt = int(tempOnes)
            tempOnesInt = str(tempOnesInt)
            binaryList.append(tempOnesInt)
        elif randomNum == 2:
            numberTwos = numberTwos + 1
            tempTwosInt = int(tempTwos)
            tempTwosInt = str(tempTwosInt)
            binaryList.append(tempTwosInt)
        else:
            print "Sorry, An Error Occured!"
        whileCount = whileCount + 1

    numberOnes = str(numberOnes)
    numberTwos = str(numberTwos)
    binaryString = ''.join(binaryList)

    print "There Were " + numberOnes + " Zeros Produced"
    print "There Were " + numberTwos + " Ones Produced"
    print ""
    print "Binary: " + binaryString
            
Comments
Sorry but there are no comments to display