Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Random six digit numbers  RSS feed

 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thanks in advance
Zein
 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saran sadaiyappan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think u can go in for system.time in milliseconds and multiply accordingly to get the six digits..
 
Venkatraman Kandaswamy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Numberformatter to the rescue:

 
Zein Nunna
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.]

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the requirements in fact usually screws up the solution.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!