1

My case is that I'm trying to find a random number pattern generator code but I really can't seem to find it. I need to use "1" , "2", "3", "4", "5" and "6" to create a random pattern with 4 digits like 1111, 1264, 2564 and etc. I tried this but it's not working.

public static void computer() {
    List<Integer> template = Arrays.asList(1, 2, 3, 4, 5, 6);
    for (int i = 0; i < 10; ++i) {
        List<Integer> items = new ArrayList<Integer>(template);
            Collections.shuffle(items);
            System.out.println(items);

    }
}
Filburt
  • 17,626
  • 12
  • 64
  • 115
Newb
  • 11
  • 3
  • 2
    Did you ask the question somewhere before? Just thinking about the sorry. Try http://stackoverflow.com/questions/363681/generating-random-integers-in-a-range-with-java – Aseem Bansal Oct 18 '15 at 08:10
  • @AseemBansal Sorry, I'm new to this and this is my first post so I don't think I have asked this question anywhere before. I tried searching but I don't couldn't find a solution for a random number pattern generator. I was stuck with this code and didn't know how to proceed any further. Therefore, I asked. Please forgive me if there was. I want to learn java programming. – Newb Oct 18 '15 at 08:46
  • No need for sorry. You had tried something and then came here. Try and read the link that I gave. It contains more explanation and alternatives. While learning don't just go with a solution that works but also look for explanation about why it works. That way you will learn better. – Aseem Bansal Oct 18 '15 at 08:50
  • @AseemBansal Thank you for your help and advice. I'm checking it out and taking it notes already. Thanks it is really resourceful! – Newb Oct 18 '15 at 08:59

3 Answers3

1

Try this out:

Random r = new Random();
int number = 0;
int d = 1;

while (number < 1000) {

    // generate number between 1-6
    int num = r.nextInt(6) + 1;

    number += num * d;
    // increase the digit (units, tens, hundreds, thousands)
    d *= 10;
}

System.out.println(number);
Ankur Shanbhag
  • 7,746
  • 2
  • 28
  • 38
  • 1
    Why number < 1000? It's four digit so should be 10000. – Aseem Bansal Oct 18 '15 at 08:15
  • Thats because, when the number is 3 digit i.e less than 1000, it goes inside the loop and generates another number and now it becomes 4 digit number. Had the check been <10000, it will go inside the loop for 4 digit numbers like 6644 and generate 5 digit number – Ankur Shanbhag Oct 18 '15 at 08:17
0
public static void computer() {
    List<Integer> template = Arrays.asList(1, 2, 3, 4, 5, 6);
    Random r = new Random();
    StringBuilder random = new StringBuilder();
    for (int i = 0; i < 4; ++i) {
       int create = r.nextInt(6);
       // find in array between 1-6 index
       int find = template.get(create);
       random.append(String.valueOf(find));
    }
    System.out.println(random);
}
Gurkan Yesilyurt
  • 2,635
  • 2
  • 20
  • 21
0
  Random r = new Random();
  String s = "";
  while(s.length() < 4)
  {
     int numpick = r.nextInt() % 6;
     if(numpick > 0)
        s += numpick;
  }

  return Integer.parseInt(s);

Can obviously be brushed up a little bit but I see this as the best strategy for developing the numbers.

Feek
  • 297
  • 3
  • 15
  • 2
    Using [`r.nextInt(6)`](https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-) instead of `r.nextInt() % 6` would be better. – fabian Oct 18 '15 at 08:40