File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Random 5-Digit number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random 5-Digit number" Watch "Random 5-Digit number" New topic
Author

Random 5-Digit number

Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
Posts: 27
How can i use Random to generate a 5-digit positive number? Any tips would be helpful.
Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
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.


Note: I love programming.
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
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


SCJP 1.4
Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
Posts: 27
Okay thanks for the help. Ill try it out when i get the time.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
You are right...i just wanted him to do some work! :-)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11398
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think we don't know whether leading zeros are allowed by the spec.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Giovanni De Stefano
Ranch Hand

Joined: Aug 17, 2004
Posts: 144
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
Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
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

Joined: Aug 17, 2004
Posts: 144
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

Joined: Dec 06, 2001
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
Clay Adkerson
Greenhorn

Joined: Mar 16, 2005
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

Joined: Aug 17, 2004
Posts: 144
Okay!

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

Giovanni
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random 5-Digit number