0
votes

I have been asked to simulate whether a printer works after every hour in one day (i.e. 24 hours). If the printer works after the hour, then it has an 90% probability of working and 10% probability of being broken at the end of the next hour.

If broken, then it has 50% probability of working or being broken the next hour.

Assume random uniform distribution and that the first hour the printer is working.

My code in Python is below:

Chance = []
Status = []
for i in range(24):
    Chance.append(random.uniform(0,1))
Chance[0] = 1
Chance

for i in Chance:
    if i > 0.1:
        Status.append('Working')
    else:
        Status.append('Broken')
Chance, Status

My problem is I can't simulate the the current event based on the previous event, i.e. if the previous event is broken how can I adjust the probability of the current event to 0.5.

3
First, you can do Chance = np.random.uniform(size=24) instead of the for loop. Second, why do you have Chance on line 6 all by itself?kevinkayaks

3 Answers

2
votes

You need to define your probability table before, like this:

                 |              NEXT STATE                |
                 |    WORKING        |      BROKEN        |
CURRENT| WORKING |      0.9          |        0.1         |
STATE  |---------|-------------------|--------------------| 
       | BROKEN  |      0.5          |        0.5         |

In order to be able to change the value very easily. You can even easily add new state. Use it like this:

import random

# The table above
table = [[.9,.1],[.5,.5]]

def simulate():
    # 0 means working, 1 means broken
    current_state = 0
    # Initialise variable
    next_state = current_state

    for i in range(24):
        # Get a number between 0 and 1
        chance = random.uniform(0, 1)

        # Given my current state (line 0 or 1), what chance do I have
        # to work the next hour ?
        if chance <= table[current_state][0]:
            # The chance variable has more chance to be under if the number
            # in the table is high.
            next_state = 0
        else:
            # BROKEN
            next_state = 1
        current_state = next_state
        print("Current state " + ("Working" if current_state == 0 else "Broken"))

if __name__ == "__main__":
    simulate()
2
votes

You should memorize the status of the printer at the previous step (hour) and adjust the threshold for calculating the random binary output:

status = []
lastStatus = True # Assume it was working

for i in range(24):
    threshold = 0.1 if lastStatus else 0.5
    lastStatus = random.uniform(0, 1) > threshold
    status.append('Working' if lastStatus else 'Broken')
0
votes

With that script:

import random

broken_probability = 0
for i in range(24):
    if random.randrange(100) > broken_probability:
        status = 'Working'
        broken_probability += 10
    else:
        status = 'Broken '
        broken_probability = 50

    print('Hour: {} - Status: {} - Broken Probability: {}'.format(str(i).zfill(2), status, broken_probability))

I've got following results:

Hour: 00 - Status: Working - Broken Probability: 10
Hour: 01 - Status: Working - Broken Probability: 20
Hour: 02 - Status: Broken  - Broken Probability: 50
Hour: 03 - Status: Broken  - Broken Probability: 50
Hour: 04 - Status: Broken  - Broken Probability: 50
Hour: 05 - Status: Broken  - Broken Probability: 50
Hour: 06 - Status: Working - Broken Probability: 60
Hour: 07 - Status: Broken  - Broken Probability: 50
Hour: 08 - Status: Broken  - Broken Probability: 50
Hour: 09 - Status: Working - Broken Probability: 60
Hour: 10 - Status: Broken  - Broken Probability: 50
Hour: 11 - Status: Broken  - Broken Probability: 50
Hour: 12 - Status: Working - Broken Probability: 60
Hour: 13 - Status: Broken  - Broken Probability: 50
Hour: 14 - Status: Broken  - Broken Probability: 50
Hour: 15 - Status: Working - Broken Probability: 60
Hour: 16 - Status: Working - Broken Probability: 70
Hour: 17 - Status: Broken  - Broken Probability: 50
Hour: 18 - Status: Broken  - Broken Probability: 50
Hour: 19 - Status: Broken  - Broken Probability: 50
Hour: 20 - Status: Broken  - Broken Probability: 50
Hour: 21 - Status: Working - Broken Probability: 60
Hour: 22 - Status: Broken  - Broken Probability: 50
Hour: 23 - Status: Working - Broken Probability: 60