from random import *

thelist = list()
newlist = list()
thesum = 0
total = 0
maxN = 10
maxRun = 1000
run = 0
v = 10
l = -v

# compound used, and.
# v a \ 2 6 \ 3 12 \ 4 10 \ 5 10 \ 10 3 \ 20 3 \ 50 0
# for a given number of random variable initial strings
# this is the minimal amount above the average we have.
# for runs 100, string size 64, and l=-v initial.

# compound used, or.
# v a \ 2 2 \ 3 0 \ 4 5 \ 5 2 \ 10 0 \ 20 2 \ 50 0
# for a given number of random variable initial strings
# this is the minimal amount above the average we have.
# for runs 100, string size 64, and l=-v initial.

def compound(a,b):
    if a == 1:
        if b == 1:
            return 1
        if b == -1:
            return 0
        if b == 0:
            return 1
    if a == -1:
        if b == 1:
            return 0
        if b == -1:
            return 1
        if b == 0:
            return -1
    if a == 0:
        if b == 1:
            return 1
        if b == -1:
            return -1
        if b == 0:
            return 0

def compoundor(a,b):
    if a == 1:
        if b == 1:
            return 1
        if b == -1:
            return 0
        if b == 0:
            return 1
    if a == -1:
        if b == 1:
            return 0
        if b == -1:
            return -1
        if b == 0:
            return -1
    if a == 0:
        if b == 1:
            return 1
        if b == -1:
            return -1
        if b == 0:
            return 0


def listtostring(oldlist):
    for m in xrange(len(oldlist)):
        if oldlist[m] == -1:
            newlist.append('F')
        if oldlist[m] == +1:
            newlist.append('T')
        if oldlist[m] == 0:
            newlist.append('0')
    return newlist




while run < maxRun:
    run += 1

    thelist = list()
    l = -v
    while l < maxN:  
        l += 1
        product = 0.0
    
        U = random()*2-1
        if -1 < U < -.3333:
            V = -1
        if -.3333 < U < .3333:
            V = 0
        if .3333 < U < +1:
            V = +1

        thelist.append(V)
        
        if l > v:
            for m in xrange(l-1):
                product = compoundor(thelist[l-1-m],product)
            thelist.append(product)
            
##    print(thelist)
##    print(listtostring(thelist))
            
    thesum = 0.0
            
    for m in xrange(maxN-1):
        thesum += thelist[m]

    total += thesum
    
    print("The Sum Result is:", thesum)
    print("The Total is:", total/run)
