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

How can I fix the size of randomly generated numbers ?

 
Shounak Kundu
Ranch Hand
Posts: 60
IBM DB2 Eclipse IDE Fedora
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am randomly generating numbers using java.util.Random . But, I can not keep the length of the numbers fixed. Can you help me please ?

Here's my code :



 
Maneesh Godbole
Saloon Keeper
Posts: 10973
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By length of number, I take it you mean the number of digits in the number.

Suppose you want only 3 digit numbers. The lowest would be 100 and highest would be 999
In pseudo code
1) Generate nextInt(1000)
2) Check if num >99. If yes, accept it, else reject it
3) Repeat
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . or myRandom.nextInt(900) + 100
 
Maneesh Godbole
Saloon Keeper
Posts: 10973
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell's solution is more elegant.
Now why didn't I think of it?
 
Shounak Kundu
Ranch Hand
Posts: 60
IBM DB2 Eclipse IDE Fedora
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys...... it has been solved...
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic