0

I need to generate 100 random 3 digit numbers. I have figured out how to generate 1 3 digit number. How do I generate 100? Here's what I have so far...

import java.util.Random;

public class TheNumbers {
    public static void main(String[] args) {
      System.out.println("The following is a list of 100 random" + 
          " 3 digit numbers.");
      Random rand= new Random();

          int pick = rand.nextInt(900) + 100;
          System.out.println(pick);


}

}

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
Chris
  • 3
  • 1
  • 2
  • 5

5 Answers5

5

The basic concept is to use a for-next loop, in which you can repeat your calculation the required number of times...

You should take a look at The for Statement for more details

Random rnd = new Random(System.currentTimeMillis());
for (int index = 0; index < 100; index++) {
    System.out.println(rnd.nextInt(900) + 100);
}

Now, this won't preclude generating duplicates. You could use a Set to ensure the uniqueness of the values...

Set<Integer> numbers = new HashSet<>(100);
while (numbers.size() < 100) {
    numbers.add(rnd.nextInt(900) + 100);
}
for (Integer num : numbers) {
    System.out.println(num);
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Can anyone explain why a hashset is being used here over a different collection type? I thought a hashset mapped a key and a value, but I can only see that a single number is being added? Also, I've tested it, and thought it would print out 100 values, but mine is printing far less.... – javapalava Mar 05 '15 at 15:15
  • OK - scratch last comment, my terminal window was only set to display a max number of values, but the first one still applies. And I still don't get how the Set ensures uniqueness? – javapalava Mar 05 '15 at 15:39
  • A Set guarantees uniqueness of its values, that's the contract. The HashSet uses the hashCode of the object to ensure that only once instance of each object exists within the Set, so while Random can generate the same number multiple times, the Set guarantees that only one number will exist – MadProgrammer Mar 05 '15 at 20:14
2

Try for loop

for(int i=0;i<100;i++)
      {
          int pick = rand.nextInt(900) + 100;
          System.out.println(pick);
      }
newuser
  • 8,338
  • 2
  • 25
  • 33
  • Yes!!!!! Thank you!!!! That worked!!!!! Now, can you tell me why it worked so I can remember for next time? – Chris Aug 30 '13 at 01:36
  • Because, you need repeated values for certain limit. so use for loop to achieve it. http://faculty.inverhills.mnscu.edu/speng/Beginning%20Java/Notes/chapter03/For.htm – newuser Aug 30 '13 at 01:42
2

If you adapt the following piece of code to your problem

    for(int i= 100 ; i < 1000 ; i++) {
        System.out.println("This line is printed 900 times.");
    }

, it will do what you want.

Mario Rossi
  • 7,651
  • 27
  • 37
  • Why would you start the index at 100? – Kendall Frey Aug 30 '13 at 01:45
  • @KendallFrey *"random 3 digit numbers"* - The question really is, why to `1000` when the OP only wanted `100` numbers, presumably between `100-999`... – MadProgrammer Aug 30 '13 at 01:46
  • @KendallFrey 1) MadProgrammer means 100-199 2) This community is tired of students having experienced professionals doing their homework. You wouldn't believe the exchange of comments I witnessed a couple of days ago. Promises of reputation points, etc. The members of this movement respond to this kind of questions by giving clues, but not final answer. – Mario Rossi Aug 30 '13 at 02:07
0

Using the answer to the question Generating random numbers in a range with Java:

import java.util.Random;

public class TheNumbers {
    public static void main(String[] args) {
      System.out.println("The following is a list of 100 random 3 digit nums.");
      Random rand = new Random();
      for(int i = 1; i <= 100; i++) {
        int randomNum = rand.nextInt((999 - 100) + 1) + 100;
        System.out.println(randomNum);
      }
}
Community
  • 1
  • 1
eKek0
  • 23,005
  • 25
  • 91
  • 119
0

This solution is an alternative if the 3-digit numbers include numbers that start with 0 (if for example you are generating PIN codes), such as 000, 011, 003 etc.

Set<String> codes = new HashSet<>(100);
Random rand = new Random();
while (codes.size() < 100)
{
   StringBuilder code = new StringBuilder();
   code.append(rand.nextInt(10));
   code.append(rand.nextInt(10));
   code.append(rand.nextInt(10));

   codes.add(code.toString());
}

for (String code : codes) 
{
    System.out.println(code);
}
aristotll
  • 8,694
  • 6
  • 33
  • 53
jbx
  • 21,365
  • 18
  • 90
  • 144