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

Postby lehongnhat314 » Tue Nov 19, 2013 10:12 pm

Hi everyone,
I need to know how to generate a random number with different digits, like 1357 or 123456789
Thank you :D
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

Postby Marbelous » Tue Nov 19, 2013 10:18 pm

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: 97
Joined: Fri May 31, 2013 8:12 pm

Re: Generate a random number with different digits

Postby metulburr » Wed Nov 20, 2013 12:16 am

New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1300
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Generate a random number with different digits

Postby lehongnhat314 » Wed Nov 20, 2013 12:16 am

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

Postby metulburr » Wed Nov 20, 2013 12:17 am

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()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1300
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Generate a random number with different digits

Postby lehongnhat314 » Wed Nov 20, 2013 1:46 am

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

Postby Mekire » Wed Nov 20, 2013 2:11 am

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
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Generate a random number with different digits

Postby ochichinyezaboombwa » Wed Nov 20, 2013 2:57 am

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: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: Generate a random number with different digits

Postby stranac » Wed Nov 20, 2013 10:46 am

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.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: Generate a random number with different digits

Postby Marbelous » Wed Nov 20, 2013 3:42 pm

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: 97
Joined: Fri May 31, 2013 8:12 pm

Re: Generate a random number with different digits

Postby lehongnhat314 » Wed Nov 20, 2013 7:06 pm

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 = 0
for i in range(len(num)):
   for j in range(i+1, len(num)):
      if num[i]==num[j]:
         nah = 1

:mrgreen:
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

Postby Marbelous » Wed Nov 20, 2013 9:49 pm

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 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 = ???????


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_stuff
True
>>> 99 in list_of_stuff
False
>>> 'spam' in list_of_stuff
True


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... :evil:

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??? :twisted: ("Join ussss....")
Marbelous
 
Posts: 97
Joined: Fri May 31, 2013 8:12 pm

Re: Generate a random number with different digits

Postby Mekire » Wed Nov 20, 2013 11:41 pm

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
User avatar
Mekire
 
Posts: 978
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Generate a random number with different digits

Postby Marbelous » Thu Nov 21, 2013 4:52 pm

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. :oops: It's definitely the tool of choice here.
Marbelous
 
Posts: 97
Joined: Fri May 31, 2013 8:12 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 2 guests