Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Bear Bibeault
• Paul Clapham
• Jeanne Boyarsky
Sheriffs:
• Devaka Cooray
• Junilu Lacar
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Tim Holloway
• Claude Moore
• Stephan van Hulst
Bartenders:
• Winston Gutkowski
• Carey Brown
• Frits Walraven

# Random 5-Digit number

Greenhorn
Posts: 27
How can i use Random to generate a 5-digit positive number? Any tips would be helpful.

Ranch Hand
Posts: 79
Try to use a loop. Like this

for (int count = 1;count<=5;count++)
{
c.print(generator.nextInt(9));
}

Note: Generator is the variable for randomize.

Ranch Hand
Posts: 161
Hi Clay,
yes, you can generate 5-digit random numbers using random(), the hint I can give you is this:

This code generates 10 random numbers with...almost always 5 digits...play with this and you will get the solution!
Good luck!

Giovanni

Greenhorn
Posts: 27
Okay thanks for the help. Ill try it out when i get the time.

Ranch Hand
Posts: 3061
What numbers contain 5 digits? Well, that would be 10000 through 99999, right? So given two numbers a and b, can you think of a way to generate a random number between them? (Hint, it can be done in a single line of code.)

Layne

Giovanni De Stefano
Ranch Hand
Posts: 161
You are right...i just wanted him to do some work! :-)

lowercase baba
Posts: 12722
51

What numbers contain 5 digits? Well, that would be 10000 through 99999, right?

I'm not sure i agree with this. doesn't 00043 have five digits?

(instanceof Sidekick)
Posts: 8791
I think we don't know whether leading zeros are allowed by the spec.

Giovanni De Stefano
Ranch Hand
Posts: 161
Hi guys,
Fred you are right BUT Clay's problem seems to be a simple assignment on how to use random()...so Layne's and my advice are supposed to be much more than enough (the next step would be to write his assignment!!!).

Giovanni

Greenhorn
Posts: 27
Sorry i took so long but i still cant really figure it out. What im trying to do is create a simple bank account, which later i will make more complex. If you dont have an account it will make a random 5-digit number that it will assign as your Account#. So it must be 10000 through 99999. I looked at the Random class but you cant specify two numbers, only one.

I'll try to mess with it a little more. I know it is something simple but i just cant seem to grasp it.
[ May 09, 2005: Message edited by: Clay Adkerson ]

Giovanni De Stefano
Ranch Hand
Posts: 161
Hi Clay,
I hope you are not getting money for this...otherwise send me a check!

This is the explanation: random() generates a number between 0.0000 and 0.99999, we want this number to be between 10000 and 99999: basically there is an offset of 10000 (that corresponds to 0.0000) so the upper limit is 99999 - offset = 99999 - 10000 = 89999

round(random() * 89999) + 10000.

Let's check the lower limit:
round(0 * 89999) + 10000 = 10000 OK!

Let's check the higher limit:
round(0.99999 * 89999) + 10000 = 99999 OK!

Don't forget round()!

Here is the code, as you can see...IT IS JUST ONE LINE!!!:

Let me know if you like it...and give me the money!!!

Layne Lund
Ranch Hand
Posts: 3061

Originally posted by Clay Adkerson:
Sorry i took so long but i still cant really figure it out. What im trying to do is create a simple bank account, which later i will make more complex. If you dont have an account it will make a random 5-digit number that it will assign as your Account#. So it must be 10000 through 99999. I looked at the Random class but you cant specify two numbers, only one.

I'll try to mess with it a little more. I know it is something simple but i just cant seem to grasp it.

[ May 09, 2005: Message edited by: Clay Adkerson ]

You're right this isn't built into the Random class, but with a little bit of thought, you should be able to implement a method like

that returns an int in between min and max. Giovanni's code above is specific for your problem, but it is probably a good idea to generalize it in case you need this for future programming projects. I'll give you a few hints: 1) How many integers are between say 10 and 20? How do you calculate how many integers are between an arbitrary min and max? 2) With the answer to hint 1, how do you get a random number that starts at the given min value?

I don't know what else I can do now except write the code for you. However, it would be more helpful for you to try and figure it out on your own, imo.

HTH

Layne

Greenhorn
Posts: 27
Okay, i get it. Thanks for all the help. I love this place . I'll try to make a method that takes two values but for now i'll use Giovanni's. Nah, im not getting anything for this lol. Its for one of my personal projects for when im bored.

Giovanni De Stefano
Ranch Hand
Posts: 161
Okay!

I am glad my code has been useful, be sure you understand the algorithm...and try to follow Layne's sugestion too!

Giovanni

 You showed up just in time for the waffles! And this tiny ad: ScroogeXHTML 8.2 - easy to use RTF to HTML converter library https://coderanch.com/t/707504/ScroogeXHTML-RTF-HTML-XHTML-converter