Possible Duplicate:
Java: generating random number in a range
I need a little help.
What code would I use to create a random number that is 5 digits long and starts with either 1 or 2?
in order to use as a company employees ID.
Thanks!!
Possible Duplicate:
Java: generating random number in a range
I need a little help.
What code would I use to create a random number that is 5 digits long and starts with either 1 or 2?
in order to use as a company employees ID.
Thanks!!
Depending on how you approach the problem something like that:
Or something like that (you probably want the instantation of the Random object of of the method but I just put it here for simplicity) :
The idea is that 1 + nextInt(2) shall always give 1 or 2. You then multiply it by 10000 to satisfy your requirement and then add a number between [0..9999].
Here's are some example output: