Hi everyone,
I need to know how to generate a random number with different digits, like 1357 or 123456789
Thank you
Yes it is homework.
All i need to do is to generate a random number with 4 different digits from 0-9(the first one is not 0).
That's all
random.randint()
metulburr wrote:Yes it is homework.
All i need to do is to generate a random number with 4 different digits from 0-9(the first one is not 0).
That's all
and the code you have already tried is where?
try:
- Code: Select all
random.randint()
lehongnhat314 wrote:I need the number to be 1234 or 1357, not 1122. It's like the number has to be different digits
nah = 0
for i in range(len(num)):
for j in range(i+1, len(num)):
if num[i]==num[j]:
nah = 1
import random # You will need the randint function from the random module
result = [] # Init your result list
first_number = str(random.randint(1, 9)) # Guarantee the first number can't be zero. Make it a string so you can join them later.
result.append(first_number) # Append it into the currently empty result list.
while len(result) < 4: # Loop until the length of result is 4 digits
next_number = ???????
>>> list_of_stuff = [1, 5, 'spam', 'Ni', 42]
>>> 5 in list_of_stuff
True
>>> 99 in list_of_stuff
False
>>> 'spam' in list_of_stuff
True
random.sample("1234567890",desired_length)
Users browsing this forum: Google [Bot], micseydel and 5 guests