Ask coding questions

← Back to all posts
##### How do we generate random numbers with an input number?

I am trying to generate random numbers (with a range) with an input number but without duplicates. How do I do that?

Answered by Coder100 (16855) [earned 5 cycles]
View Answer
##### Comments
hotnewtop
Coder100 (16855)

random numbers?

wdym by input?

you can generate numbers without clashing like this:

``````import random
numbers = []

num = random.randint(1, 5)
while num in numbers:
num = random.randint(1, 5)

numbers.append(num)

print(num)``````
seohyeonl (0)

@Coder100 I want to generate numbers randomly while always including a specific number. For example, if I generate 5 numbers randomly from 1 to 10, one of the 5 numbers has to be 2 always. How do I do that?

Coder100 (16855)

just make it so that in the array, the first item is that specific number @seohyeonl

seohyeonl (0)

Can you explain that a little bit? @Coder100

Coder100 (16855)

like from my example,

``````import random
firstnumber = ... # add it here
numbers = [firstnumber]

for _ in range(5):
num = random.randint(1, 10)
while num in numbers:
num = random.randint(1, 10)

numbers.append(num)

print(numbers)``````
seohyeonl (0)

but that created this:

``````['22', 6]
['22', 6, 13]
['22', 6, 13, 21]
['22', 6, 13, 21, 41]
['22', 6, 13, 21, 41, 23]``````

Something like this. (The numbers are different because I changed them.) But what I want is just one list. Can you help me? @Coder100

Coder100 (16855)

you need to turn the input into an int. @seohyeonl

Coder100 (16855)

also you must have done it wrong, it only stores one var @seohyeonl

Coder100 (16855)

also why are you importing random so many times xd @seohyeonl

seohyeonl (0)

This was the code that I originally made. But this made duplicates so I had to change it.
Here's the code:

``````x2 = input()
print("")
print("Great! You will be able to get your lucky numbers soon. Anwer Let's go! when you are ready.")
Q2 = input()
if Q2 == "Let's go!":
print("")
import random
randomlist = random.sample(range(1, 49), 5)
randomlist.insert(0, x2)
print(randomlist)
print("")``````

And this is the altered version of the code from you. Would you mind looking at it and tell me what I have done wrong?

``````x2 = input()
print("")
print("Great! You will be able to get your lucky numbers soon. Anwer Let's go! when you are ready.")
Q2 = input()
if Q2 == "Let's go!":
import random
firstnumber = x2
numbers = [firstnumber]
for i in range(5):
num = random.randint(1, 49)
while num in numbers:
num = random.randint(1, 49)

numbers.append(num)
print(numbers)``````
Coder100 (16855)

you put the print function inside the loop while it was still generating the numbers. @seohyeonl

seohyeonl (0)

Oh, ok thank you! I have another question. How do we reset the text colour?
For example, this is my code:

``````from time import sleep
from colorama import *
import sys

print("Whatever it takes- Your team is " + Back.RED + Fore.BLACK + canada_teams + Back.BLACK + Fore.WHITE)

print("First, please write your name here")``````

Even if I make the background black, the background of "First, please write your name here" appears as grey. I want to reset the background on this sentence. How can I do that?
@Coder100

Coder100 (16855)

@seohyeonl `Style.RESET_ALL`