9 minute read

python code positing test

from random import *
[randint(0,99) for i in range(8)]
[88, 45, 14, 36, 89, 59, 8, 48]
[random() for i in range(5)]
[0.29519557926205764,
 0.5785629961217171,
 0.3345763576406169,
 0.03523563327894497,
 0.8902222677943041]
[uniform(1,10) for i in range(5)]
[6.264799917178367,
 6.400925584281083,
 3.756581136706882,
 5.727410878701661,
 6.183625278984308]
[randrange(10) for i in range(5)]
[3, 6, 1, 2, 1]
colors = ['red','blue','green','gray','black']
choice(colors)
'gray'
sample(colors,2)
['blue', 'green']
list = [1,2,3,4,5,6,7]
shuffle(list)
list
[7, 2, 6, 1, 4, 3, 5]
def roll():
    from random import randint
    return randint(1,6)
def DiceStat(trials):
    count_list = [0,0,0,0,0,0,0,0,0,0,0]
    count_prob = [0,0,0,0,0,0,0,0,0,0,0]
    
    for i in range(trials):
        value1 = roll()
        value2 = roll()
        dice_index = value1 + value2 -2
        count_list[dice_index] = count_list[dice_index] + 1
        
    for j in range(0,11):
        count_prob[j] = count_list[j] / trials
        print("The probability for", j+2, ":", count_prob[j])
DiceStat(10000)
The probability for 2 : 0.0255
The probability for 3 : 0.0578
The probability for 4 : 0.0827
The probability for 5 : 0.1123
The probability for 6 : 0.1339
The probability for 7 : 0.1681
The probability for 8 : 0.1345
The probability for 9 : 0.1176
The probability for 10 : 0.0814
The probability for 11 : 0.0604
The probability for 12 : 0.0258
import random
def diceThrowingCount(boardsize):
    start = 0
    count = 0
    while(True):
        start = start + random.randint(1,6)
        if start > boardsize:
            break
        count += 1
    return count
total = 0
for boardsize in range(40,61,2):
    for trial in range(1000):
        total = total + diceThrowingCount(boardsize)
    print("Board size" + str(boardsize) + ":Average " + str(round(total/1000)) + "throwings.")
    total = 0 
Board size40:Average 11throwings.
Board size42:Average 12throwings.
Board size44:Average 12throwings.
Board size46:Average 13throwings.
Board size48:Average 13throwings.
Board size50:Average 14throwings.
Board size52:Average 15throwings.
Board size54:Average 15throwings.
Board size56:Average 16throwings.
Board size58:Average 16throwings.
Board size60:Average 17throwings.
import random
def prepare_bag(total,hit):
    lst = [] 
    for i in range(hit):
        lst.append(1)
    for i in range(total - hit):
        lst.append(0)
    random.shuffle(lst)
    return lst
def stat_lottery(total,hit):
    cntTotal = 0
    
    for i in range(1000):
        lst = prepare_bag(total,hit)
        
        cnt = 0
        while(True):
            if lst[cnt] == 1:
                break
            cnt +=1
        cntTotal = cntTotal + cnt
    
print("-----------")
print("제비의 수: " + str(total) , "당첨의 수: " + str(hit))
print("뽑기 확률" + str(hit/total*100) + "%의 제비뽑기에서")
print("당첨을 뽑기 위해서는 평균적으로")
print(str(round(cntTotal/1000)) + "장을 뽑아야 한다.")

for total, hit in [(1000,5),(10000,50),(100000,500)]:
    stat_lottery(total,hit)
-----------



---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-19-53c5c7815337> in <module>
     13 
     14 print("-----------")
---> 15 print("제비의 수: " + str(total) , "당첨의 수: " + str(hit))
     16 print("뽑기 확률" + str(hit/total*100) + "%의 제비뽑기에서")
     17 print("당첨을 뽑기 위해서는 평균적으로")


NameError: name 'total' is not defined

The Hungry Dice-Player Problem

def roll():
    from random import randint
    return randint(1,6)

def dice_game():
    strikes = 0
    winnings = 0
    while strikes < 3:
        die1 = roll()
        die2 = roll()
        if die1 == die2:
            strikes +=1
        else:
            winnings += die1 + die2
    return winnings

def average_winnings(runs):
    total = 0
    for n in range(runs):
        total = total + dice_game()
    return total / runs
[ round(average_winnings(10000),2) for i in range(5)]
[105.5, 105.17, 104.85, 105.25, 104.36]

Clueless Student Problem

from random import shuffle
nums = list(range(10))
nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shuffle(nums)
nums
[7, 4, 8, 5, 0, 1, 3, 9, 2, 6]
from random import shuffle
def clue_student(pairs, trials):
    num_correct = 0
    matching = list(range(pairs))
    for i in range(trials):
        shuffle(matching)
        for j in range(pairs):
            if matching[j] == j:
                num_correct = num_correct + 1
    return num_correct / trials
clue_student(20,10000)
0.9694

Umbrella Quandary Problem

from random import random
def q_umbrella(p) : 
    wet = False
    trips = 0
    location = 0
    umbrellas = [1,1]
    
    while (not wet):
        if random() < p:
            if umbrellas[location] == 0:
                wet = True
            else:
                trips += 1
                umbrellas[location] -= 1
                location = 1 - location
                umbrellas[location] += 1
                
        else:
            trips += 1
            location = 1 - location
    
    return trips
            
q_umbrella(0.5)
4
def q_umb_test():
    results = [None] * 99
    prob = 0.01
    for i in range(99):
        trips = 0
        for k in range(10000):
            trips = trips + q_umbrella(prob)
        results[i] = trips / 10000
        prob = prob + 0.01
    return results
probability_list = q_umb_test()
for j in range(1,100):
    print("The Number of non-wet trips under the probability", j, "%", probability_list[j-1])
The Number of non-wet trips under the probability 1 % 398.6327
The Number of non-wet trips under the probability 2 % 201.1698
The Number of non-wet trips under the probability 3 % 133.7767
The Number of non-wet trips under the probability 4 % 100.0875
The Number of non-wet trips under the probability 5 % 80.9435
The Number of non-wet trips under the probability 6 % 68.0684
The Number of non-wet trips under the probability 7 % 57.7875
The Number of non-wet trips under the probability 8 % 51.2439
The Number of non-wet trips under the probability 9 % 45.2553
The Number of non-wet trips under the probability 10 % 41.2194
The Number of non-wet trips under the probability 11 % 38.7023
The Number of non-wet trips under the probability 12 % 34.901
The Number of non-wet trips under the probability 13 % 31.9462
The Number of non-wet trips under the probability 14 % 30.2384
The Number of non-wet trips under the probability 15 % 27.793
The Number of non-wet trips under the probability 16 % 26.0022
The Number of non-wet trips under the probability 17 % 25.0612
The Number of non-wet trips under the probability 18 % 23.6781
The Number of non-wet trips under the probability 19 % 22.6704
The Number of non-wet trips under the probability 20 % 21.297
The Number of non-wet trips under the probability 21 % 20.8031
The Number of non-wet trips under the probability 22 % 19.6651
The Number of non-wet trips under the probability 23 % 19.0674
The Number of non-wet trips under the probability 24 % 18.1726
The Number of non-wet trips under the probability 25 % 17.7755
The Number of non-wet trips under the probability 26 % 17.2519
The Number of non-wet trips under the probability 27 % 16.7097
The Number of non-wet trips under the probability 28 % 16.1505
The Number of non-wet trips under the probability 29 % 15.7279
The Number of non-wet trips under the probability 30 % 15.1354
The Number of non-wet trips under the probability 31 % 14.9407
The Number of non-wet trips under the probability 32 % 14.3049
The Number of non-wet trips under the probability 33 % 14.2777
The Number of non-wet trips under the probability 34 % 13.7704
The Number of non-wet trips under the probability 35 % 13.4014
The Number of non-wet trips under the probability 36 % 13.2065
The Number of non-wet trips under the probability 37 % 12.8141
The Number of non-wet trips under the probability 38 % 12.8351
The Number of non-wet trips under the probability 39 % 12.7076
The Number of non-wet trips under the probability 40 % 12.3137
The Number of non-wet trips under the probability 41 % 11.847
The Number of non-wet trips under the probability 42 % 11.8953
The Number of non-wet trips under the probability 43 % 11.6437
The Number of non-wet trips under the probability 44 % 11.6881
The Number of non-wet trips under the probability 45 % 11.4406
The Number of non-wet trips under the probability 46 % 11.4613
The Number of non-wet trips under the probability 47 % 11.3508
The Number of non-wet trips under the probability 48 % 11.1389
The Number of non-wet trips under the probability 49 % 11.2297
The Number of non-wet trips under the probability 50 % 10.9687
The Number of non-wet trips under the probability 51 % 10.9837
The Number of non-wet trips under the probability 52 % 10.8362
The Number of non-wet trips under the probability 53 % 10.8795
The Number of non-wet trips under the probability 54 % 10.8107
The Number of non-wet trips under the probability 55 % 10.6249
The Number of non-wet trips under the probability 56 % 10.5531
The Number of non-wet trips under the probability 57 % 10.6917
The Number of non-wet trips under the probability 58 % 10.515
The Number of non-wet trips under the probability 59 % 10.6499
The Number of non-wet trips under the probability 60 % 10.584
The Number of non-wet trips under the probability 61 % 10.6211
The Number of non-wet trips under the probability 62 % 10.8122
The Number of non-wet trips under the probability 63 % 10.7739
The Number of non-wet trips under the probability 64 % 10.7315
The Number of non-wet trips under the probability 65 % 10.6817
The Number of non-wet trips under the probability 66 % 10.98
The Number of non-wet trips under the probability 67 % 11.1115
The Number of non-wet trips under the probability 68 % 11.1038
The Number of non-wet trips under the probability 69 % 11.2275
The Number of non-wet trips under the probability 70 % 11.7389
The Number of non-wet trips under the probability 71 % 11.5547
The Number of non-wet trips under the probability 72 % 11.8121
The Number of non-wet trips under the probability 73 % 11.8684
The Number of non-wet trips under the probability 74 % 12.1606
The Number of non-wet trips under the probability 75 % 12.4599
The Number of non-wet trips under the probability 76 % 12.7674
The Number of non-wet trips under the probability 77 % 12.7573
The Number of non-wet trips under the probability 78 % 13.2906
The Number of non-wet trips under the probability 79 % 13.3722
The Number of non-wet trips under the probability 80 % 13.9757
The Number of non-wet trips under the probability 81 % 14.5581
The Number of non-wet trips under the probability 82 % 14.9423
The Number of non-wet trips under the probability 83 % 15.9176
The Number of non-wet trips under the probability 84 % 16.2284
The Number of non-wet trips under the probability 85 % 17.3611
The Number of non-wet trips under the probability 86 % 17.9952
The Number of non-wet trips under the probability 87 % 18.9123
The Number of non-wet trips under the probability 88 % 20.1981
The Number of non-wet trips under the probability 89 % 21.1729
The Number of non-wet trips under the probability 90 % 23.4404
The Number of non-wet trips under the probability 91 % 25.1786
The Number of non-wet trips under the probability 92 % 28.4019
The Number of non-wet trips under the probability 93 % 31.8555
The Number of non-wet trips under the probability 94 % 36.0993
The Number of non-wet trips under the probability 95 % 42.8175
The Number of non-wet trips under the probability 96 % 53.6476
The Number of non-wet trips under the probability 97 % 69.613
The Number of non-wet trips under the probability 98 % 102.6783
The Number of non-wet trips under the probability 99 % 203.5734
for i in range(5):
    print(i)
0
1
2
3
4

16-A

import random
def f1(n):
    area = 0
    for i in range(n):
        j = 10/n*(i+1)
        s = (j**5 - j**4) * 10/n
        area += s
    print(area)

for i in [10,100,1000,10000,100000,1000000]:
    f1(i)
195492.0
151204.99920000005
147117.04999992007
146711.67050000004
146671.16670500068
146667.11666705066
import random

def f2(n):
    cir = 0
    sq = 0
    r = 0.5 
    
    for i in range(n):
        x = random.uniform (-0.5,0.5)
        y = random.uniform (-0.5,0.5)
        
        if x ** 2 + y ** 2 <= (1/4):
            cir += 1
        sq += 1
    return 4*(cir / sq)

for i in [10,100,1000,10000,100000,1000000,10000000]:
    print(f2(i))
2.8
3.24
3.128
3.1352
3.14508
3.139584
3.1415196








Updated: