Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random 5-Digit number

 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i use Random to generate a 5-digit positive number? Any tips would be helpful.
 
Manuel Diaz
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Clay Adkerson
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks for the help. Ill try it out when i get the time.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right...i just wanted him to do some work! :-)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we don't know whether leading zeros are allowed by the spec.
 
Giovanni De Stefano
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay!

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

Giovanni
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic