This exercise is a simulation of a ship arrival system playing with random numbers. Initially, a random number is run to know how many ships arrive during the day. immediately another is generated to see how many ships from which they arrived can unload merchandise. Those who could not download that day, may do so the next day, and it will accumulate to the next day's boats. an example of what the program would be:
# ALEAT BARCOS ACUM ALEAT2 DESCARGADOS RESTANTE
1 0.678 3 0 0.823 2 1
2 0.806 4 1 0.401 3 2
3 0.967 5 2 0.785 5 2
4 0.116 1 2 0.463 2 1
5 0.760 4 1 0.850 4 1
6 0.103 0 1 0.019 0 1
7 0.849 4 1 0.524 2 3
8 0.566 3 3 0.215 3 3
9 0.567 4 3 0.936 4 3
10 0.122 5 3 0.398 4 4
11 0.455 1 4 0.533 1 4
12 0.165 1 4 0.883 1 4
13 0.031 2 4 0.983 2 4
14 0.571 5 4 0.357 3 6
15 0.513 1 6 0.835 1 6
In the code I have, I generate a random # to know how many ships come to download, the downloaded variable should not be greater than the number of ships that arrive. my idea was to do a while
to generate another random # until a smaller amount was generated to arriving ships.
When I run the program, it freezes trying to generate a random number that is less than the variable barcos
, but there are times when it runs normally.
from random import random
i=1
Acum = 0
count = 0
barcos = 0
sumaacum = 0
print("#\tALEAT\tBARCOS\tACUM\tSumaAcum\tALEAT\tBdESCAR\tREST")
while i <= 15:
aleat=random()
if aleat < 0.13:
barcos = 0
elif aleat < 0.3:
barcos = 1
elif aleat < 0.45:
barcos = 2
elif aleat < 0.7:
barcos = 3
elif aleat < 0.9:
barco = 4
else:
barcos = 5
while True:
aleat2 = random()
if aleat2 < 0.05:
descargados = 1
elif aleat2 < 0.2:
descargados = 2
elif aleat2 < 0.7:
descargados = 3
elif aleat2 < 0.9:
descargados = 4
else:
descargados = 5
if descargados > barcos:
aleat2 = random()
else:
break
sumaacum = barcos + Acum
restan = sumaacum - descargados
count += restan
print("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}".format(i,str(aleat)[0:5],barcos,Acum,sumaacum,str(aleat2)[0:5],descargados,restan))
Acum = restan
i+=1
print("el total de barcos con retardo son: {}".format(count))
the output of the program is:
# ALEAT BARCOS ACUM SumaAcum ALEAT BdESCAR REST
1 0.426 2 0 2 0.199 2 0
2 0.866 2 0 2 0.000 1 1
3 0.536 3 1 4 0.066 2 2
4 0.679 3 2 5 0.154 2 3
5 0.857 3 3 6 0.673 3 3
6 0.983 5 3 8 0.646 3 5
7 0.772 5 5 10 0.028 1 9
8 0.520 3 9 12 0.516 3 9
9 0.748 3 9 12 0.109 2 10
Traceback (most recent call last):
File "c:\Users\cisco\Desktop\tempCodeRunnerFile.py", line 30, in <module>
descargados = 2
KeyboardInterrupt
On this occasion, the program is trying to generate a random # talque boats descargados
is less than barcos
that arrive. I had to stop the program by pressing Ctrl + c to run the program again.
# ALEAT BARCOS ACUM SumaAcum ALEAT BdESCAR REST
1 0.335 2 0 2 0.052 2 0
2 0.490 3 0 3 0.605 3 0
3 0.830 3 0 3 0.643 3 0
4 0.492 3 0 3 0.657 3 0
5 0.360 2 0 2 0.169 2 0
6 0.503 3 0 3 0.560 3 0
7 0.752 3 0 3 0.327 3 0
8 0.508 3 0 3 0.518 3 0
9 0.500 3 0 3 0.407 3 0
10 0.971 5 0 5 0.003 1 4
11 0.503 3 4 7 0.522 3 4
12 0.581 3 4 7 0.170 2 5
13 0.549 3 5 8 0.103 2 6
14 0.183 1 6 7 0.035 1 6
15 0.767 1 6 7 0.006 1 6
el total de barcos con retardo son: 31
In this run, the program ran smoothly.
How else can I validate so that it does not form infinite loops and avoid these incidents? Thanks