programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Random six digit numbers

Ranch Hand
Posts: 245
Guys, I have a little issue.

Basicaly I want to creat random numbers that are six digits and integers.

Now i have no problem in creating random numbers, i do the following

(I use the round method that returns in long format hence whole number)

Now this gives me values like 52127 , xxxx, xyzh etc..

I want it to be in six digit format like 052652 or 001025..

Any idea I could resolve this issue?

Zein

Ranch Hand
Posts: 502
DO you want to display the number?
If you are keeping the number as an int then 012345 = 12345
If you are converting the number to String, you can simply convert the number first and then pad zeroes on the left till your String is 6 chars in length

Ranch Hand
Posts: 39
I think u can go in for system.time in milliseconds and multiply accordingly to get the six digits..

Ranch Hand
Posts: 120
Numberformatter to the rescue:

Zein Nunna
Ranch Hand
Posts: 245
Cheers guys, that helped alot. I found another way too, buy adding the minimum value to the random number generated,

See code below [//comments ref the website where I got it from.]

author
Sheriff
Posts: 23364
127

Originally posted by Zein Nunna:
Cheers guys, that helped alot. I found another way too, buy adding the minimum value to the random number generated,

See code below [//comments ref the website where I got it from.]

Hate to point out the obvious, but you solved the problem by changing the requirements. This is not a common luxury with projects.

Henry

Ranch Hand
Posts: 214
Changing the requirements in fact usually screws up the solution.

Sheriff
Posts: 11343

Originally posted by Henry Wong:
... Hate to point out the obvious, but you solved the problem by changing the requirements. This is not a common luxury with projects.

No, but it's a good "business" skill.