## Generate a random number with different digits

This is the place for queries that don't fit in any of the other categories.

### Generate a random number with different digits

Hi everyone,
I need to know how to generate a random number with different digits, like 1357 or 123456789
Thank you
Last edited by Mekire on Wed Nov 20, 2013 2:04 am, edited 1 time in total.
Reason: Lcok.
lehongnhat314

Posts: 7
Joined: Sat Oct 19, 2013 11:52 pm

### Re: Generate a random number with different digits

There are several ways to do this and it's really very straightforward. Do you need to specify the number of digits? The range?

Please post your current attempt so we can see what you're trying. (This sounds like a homework question so I would rather not just post a solution. )

Marbelous

Posts: 401
Joined: Fri May 31, 2013 8:12 pm

### Re: Generate a random number with different digits

we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Generate a random number with different digits

Ty xD
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
Last edited by Mekire on Wed Nov 20, 2013 2:05 am, edited 1 time in total.
Reason: Lock.
lehongnhat314

Posts: 7
Joined: Sat Oct 19, 2013 11:52 pm

### Re: Generate a random number with different digits

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()`
we will be moving to python-forum.io on October 1 2016
more details here

metulburr

Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

### Re: Generate a random number with different digits

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()`

I need the number to be 1234 or 1357, not 1122. It's like the number has to be different digits
Last edited by Mekire on Wed Nov 20, 2013 2:06 am, edited 1 time in total.
Reason: Lock.
lehongnhat314

Posts: 7
Joined: Sat Oct 19, 2013 11:52 pm

### Re: Generate a random number with different digits

Your task is easily accomplished. You however still haven't shown any attempt. Until you do you won't get much more help.

In fact, if you make another post with no attempt whatsoever, I'm locking your thread.

Try using random.shuffle on a list of characters. Try using itertools. Try anything. Just try.

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Generate a random number with different digits

I am curious why you would use itertools... although it is of course possible with itertools. (Please PM me (if that sounds appropriate) if you think it's better).

Also, here are my additional requirements to make this more interesting:
a) no itertools;
b) no shuffle;
c) no ifs allowed.

PS: that's of course not to do the OP's homework; maybe for sometime later.
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

### Re: Generate a random number with different digits

lehongnhat314 wrote:I need the number to be 1234 or 1357, not 1122. It's like the number has to be different digits

Just check the number; if it has repeated digits, get another one.
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

### Re: Generate a random number with different digits

HINT: It will probably be easier to build your number as a list of strings and then join the list together and convert it to an integer.

Some pseudo-code to get you started:

Import the random module.
Initialize an empty list called result and an integer called count.
Start a while loop that doesn't end until you break out of it. i.e.: while True:
Get a new random integer to test. newNum = str(random.randint(0, 9)
Test newNum to be sure it's not zero and not already in the result.
If not, append it into result and add 1 to count.
Test to see if result contains 4 digits.
If count = 4 ... break out of loop
Last step is to join the list into an integer number. There is a string method called join that will help you. "".join(result) will join together the items in a list with nothing between because we are joining with the SPACE character. Use the int() function to convert your 4 digit string into an integer.

See what you can come up with and post it and we'll help you along...

Marbelous

Posts: 401
Joined: Fri May 31, 2013 8:12 pm

### Re: Generate a random number with different digits

All I can think of is to convert the number into an array and then check if there's any duplicated element, like this
Code: Select all
`nah = 0for i in range(len(num)):   for j in range(i+1, len(num)):      if num[i]==num[j]:         nah = 1`

Last edited by Mekire on Wed Nov 20, 2013 11:19 pm, edited 1 time in total.
Reason: Lock.
lehongnhat314

Posts: 7
Joined: Sat Oct 19, 2013 11:52 pm

### Re: Generate a random number with different digits

That's far more complicated than it needs to be. Just build the 4 digit number 1 digit at a time, each time making sure the new number hasn't already been been used. When the length = 4 you're done.

Since you're just beginning, I would do this with a simple, step-by-step, brute force method.

Code: Select all
`import random  # You will need the randint function from the random moduleresult = [] # Init your result listfirst_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 = ???????`

HINT: Your "next_number" needs to be between 0 and 9 this time. AND it needs to be unique. There is a useful Python keyword called 'in' that can check if the next_number has already been used.
See how it works:
Code: Select all
`>>> list_of_stuff = [1, 5, 'spam', 'Ni', 42]>>> 5 in list_of_stuffTrue>>> 99 in list_of_stuffFalse>>> 'spam' in list_of_stuffTrue`

Obviously, you'll need to code something that translates to " IF the Next Number is NOT IN the Result, then append it into the result. Try and see what you come up with...

Also, you'll be left with a list of 4 strings that represent integers when you're done. How will you turn that into the 4 digit output that you desire??? ("Join ussss....")

Marbelous

Posts: 401
Joined: Fri May 31, 2013 8:12 pm

### Re: Generate a random number with different digits

Being that he has no choice but to use the random module anyway, I really think he should select the right tool for the job.

Code: Select all
`random.sample("1234567890",desired_length)`
Will get you almost all the way. The only thing left is to throw out results that start with "0".

-Mek
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

### Re: Generate a random number with different digits

Yeah, I was going to suggest random shuffle on a list of integers and then slicing off 4 of them. I figured I might as well use some loops and conditionals to show the basics since the OP is obviously new to Python. There's always time to optimize it down to a one liner later when he learns some more advanced techniques.

I did totally forget about random.sample though. It's definitely the tool of choice here.

Marbelous

Posts: 401
Joined: Fri May 31, 2013 8:12 pm