コード例 #1
0
def generator_Of_Random_Coin(Seedd, Probb, Ntosss, Nexpp, Outputt):
    #Assignments of defaults
    Seed = 53422

    # Single coin-toss probability for "1"
    Prob = 0.3

    # number of coin tosses (per experiment)
    Ntoss = 10

    # number of experiments
    Nexp = 10

    # output file defaults
    doOutputFile = False

    # read the user-provided seed from the command line (if there)
    if '-seed' in sys.argv:
        p = sys.argv.index('-seed')
        seed = sys.argv[p+1]
    if '-prob' in sys.argv:
        p = sys.argv.index('-prob')
        ptemp = float(sys.argv[p+1])
        if ptemp >= 0 and ptemp <= 1:
            prob = ptemp
    if '-Ntoss' in sys.argv:
        p = sys.argv.index('-Ntoss')
        Nt = int(sys.argv[p+1])
        if Nt > 0:
            Ntoss = Nt
    if '-Nexp' in sys.argv:
        p = sys.argv.index('-Nexp')
        Ne = int(sys.argv[p+1])
        if Ne > 0:
            Nexp = Ne
    if '-output' in sys.argv:
        p = sys.argv.index('-output')
        OutputFileName = sys.argv[p+1]
        doOutputFile = True

    # class instance of our Random class using seed
    Seed = int(Seedd)
    Prob = float(Probb)
    Ntoss = int(Ntosss)
    Nexp = int(Nexpp)
    random = Random(Seed)
    doOutputFile = True
    
    if doOutputFile == True:
         outfile = Outputt 
    else:
        outfile = "Flips.txt"
    
    if doOutputFile: #flag to check if there is file
        outfile = open(outfile, 'w+')
        for e in range(0,Nexp):
            for t in range(0,Ntoss):
                outfile.write(str(random.Geometric(Prob))+" ")
            outfile.write(" \n")
        outfile.close()
    else:
        for e in range(0,Nexp):
            for t in range(0,Ntoss):
                print(random.Geometric(Prob), end=' ')
            print(" ")
    return (outfile)