dimanche 24 septembre 2017

Multiple if statements to categorize a random number in a partitioned interval

I am partitioning a random number to be in one of 2 cases to simulate a roll of a die. The problem is that sometimes, there is more than one step per loop. Please see the MWE below:


    count = 0
    n = random.random()

    while count = 1/2:
            n = random.random() # generate a new random number
            print("   Tails")

        count = count + 1

Output


    Count = 0
       Heads
       Tails
    Count = 1
       Heads
    Count = 2
       Heads
       Tails
    Count = 3
       Heads
       Tails
    Count = 4
       Heads
    Count = 5
       Heads
    Count = 6
       Heads
       Tails
    Count = 7
       Tails
    Count = 8
       Tails
    Count = 9
       Tails
    Count = 10
       Tails




Aucun commentaire:

Enregistrer un commentaire