def random(n, p):
    #a = stdarray.create2D(n, n, False)
    a = [[[False for k in range(n)] for col in range(n)] for row in range(n)]
    for i in range(n):
        for j in range(n):
            for k in range(n):
                a[i][j][k] = stdrandom.bernoulli(p)
    return a
Exemplo n.º 2
0
def random(n, p):
    a = stdarray.create2D(n, n, False)
    for i in range(n):
        for j in range(n):
            a[i][j] = stdrandom.bernoulli(p)
    #方格的横线比竖线少一
    for i in range(0, n, 2):
        for j in range(int((n - 1) / 2), n):
            a[i][j] = False
    return a
Exemplo n.º 3
0
def main():

    n = int(sys.argv[1])
    p = float(sys.argv[2])

    heads = Counter("Heads", n)
    tails = Counter("Tails", n)

    for i in range(n):
        if stdrandom.bernoulli(p):
            heads.increment()
        else:
            tails.increment()

    stdio.writeln(heads)
    stdio.writeln(tails)
Exemplo n.º 4
0
def main():

    n = int(sys.argv[1])
    p = float(sys.argv[2])
    
    heads = Counter('Heads', n)
    tails = Counter('Tails', n)
    
    for i in range(n):
        if stdrandom.bernoulli(p):
            heads.increment()
        else:
            tails.increment()
    
    stdio.writeln(heads)
    stdio.writeln(tails)
Exemplo n.º 5
0
def random(n, p):
    a = stdarray.create2D(n, n, False)
    for i in range(n):
        for j in range(n):
            a[i][j] = stdrandom.bernoulli(p)
    return a
Exemplo n.º 6
0
def binomial(n):
    heads = 0
    for i in range(n):
        if stdrandom.bernoulli(0.5):
            heads += 1
    return heads
Exemplo n.º 7
0
stake = int(sys.argv[1])
goal = int(sys.argv[2])
trials = int(sys.argv[3])
p = eval(sys.argv[4])

# Run trialCount experiments that start with stake and terminate on
# 0 or goal.
bets = 0
wins = 0
for t in range(trials):
    # Run one experiment.
    cash = stake
    while cash > 0 and cash < goal:
        # Simulate one bet.
        bets += 1
        if stdrandom.bernoulli(p):
            cash += 1
        else:
            cash -= 1
    if cash == goal:
        wins += 1

stdio.writeln(str(100 * wins // trials) + '% wins')
stdio.writeln('Avg # bets: ' + str(bets // trials))

#-----------------------------------------------------------------------

# python gambler.py 10 20 1000
# 49% wins
# Avg # bets: 99
Exemplo n.º 8
0
def random(n, p):
    a = stdarray.create2D(n, n, False)
    for i in range(n):
        for j in range(n):
            a[i][j] = stdrandom.bernoulli(p)
    return a