How can I print out more than one random result without possible duplicates in any print statements

Viewed 35

I am doing a basic random number generator which prints out the fist 6 numbers in a range up to 45 which works fine but I need it to print out a second time with 2 numbers up to 45, these two numbers can not be the same as any of the 6 numbers either.

enter code here
import random

nums = range(1, 45)
supps = range(1,45)

print(random.sample(nums,6))
print(random.sample(supps,2))
enter code here
1 Answers

You can samply generate 8 random numbers and put them in array, then print the first 6 numbers and 2 last numbers :

import random

nums = range(1, 45)
random_nums = random.sample(nums,8)
print('First six numbers : ',random_nums[:6],'Last two numbers : ',random_nums[6:])

NB : idk if you intend to use the "range" function in that way or you declared it somewhere else, because at that way it wont work, do this instead :

nums = []
for i in range(1,46):
    nums.append(i)
Related