How do I write code in order to generate random numbers in a way that a random number can only be printed once every 5 times
Question:
I’ve written code in python in order to generate random numbers between 110-115, 19 times. The code works however I would like to add code that states that the same random numbers cannot be printed out next to each other, they can only be printed out after the other 5 numbers have been used. for example:
[112, 113, 115, 110, 111, 114, 112, 113, 115, 110, 111, 114…]
until 19 values have been printed.
I have the following code:
randomlist = []
for i in range(19):
n = random.randint(110,115)
randomlist.append(n)
print(*randomlist, sep = "n")
Answers:
Try this, to generate 19 nos where no repetition in 5 consecutive nos.
import random
randomlist = []
while len(randomlist) < 19:
n = random.randint(110,115)
if n not in randomlist[-5:]:
randomlist.append(n)
else:
continue
Hope this helps…
Try this below code , it will solve your problem.
import random
randomlist = []
i = 0
number_of_values = 19
while len(randomlist) < number_of_values:
n = random.randint(110,115)
if n not in randomlist[-5:]:
randomlist.append(n)
print(randomlist, sep = "n")
I’ve written code in python in order to generate random numbers between 110-115, 19 times. The code works however I would like to add code that states that the same random numbers cannot be printed out next to each other, they can only be printed out after the other 5 numbers have been used. for example:
[112, 113, 115, 110, 111, 114, 112, 113, 115, 110, 111, 114…]
until 19 values have been printed.
I have the following code:
randomlist = []
for i in range(19):
n = random.randint(110,115)
randomlist.append(n)
print(*randomlist, sep = "n")
Try this, to generate 19 nos where no repetition in 5 consecutive nos.
import random
randomlist = []
while len(randomlist) < 19:
n = random.randint(110,115)
if n not in randomlist[-5:]:
randomlist.append(n)
else:
continue
Hope this helps…
Try this below code , it will solve your problem.
import random
randomlist = []
i = 0
number_of_values = 19
while len(randomlist) < number_of_values:
n = random.randint(110,115)
if n not in randomlist[-5:]:
randomlist.append(n)
print(randomlist, sep = "n")