keitamike
keitamike

Reputation: 179

Generating random numbers with identical pairs between 1 to 8?

Like my question, i need to generate random numbers that have identical pairs between a range. i have tried to generate random numbers and stored in an array but the numbers are repeating more than twice. i have 16 random numbers to be generated in the range. Any idea how to make it generate only identical pairs random number?

Upvotes: 3

Views: 2673

Answers (4)

aliasbody
aliasbody

Reputation: 836

I have made this way :

    Random random = new Random();
    List<Integer> listaCartoes = new ArrayList<Integer>();

    for(int i=0; i<8;)
    {
       int r = random.nextInt(8) + 1;
       if(!listaCartoes.contains(r)) 
       {
          listaCartoes.add(r);
          listaCartoes.add(r);
          ++i;
       }
    }

    Collections.shuffle(listaCartoes);

Hope it helps ^_^

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383786

I believe this clearly shows you how to approach the problem:

public static List<Integer> shuffled8() {
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 1; i <= 8; i++) {
        list.add(i);
    }
    Collections.shuffle(list);
    return list;
}

public static void main(String[] args) {
    List<Integer> first = shuffled8();
    List<Integer> second= shuffled8();
    for (int i = 0; i < 8; i++) {
        System.out.println(first.get(i) + " " + second.get(i));
    }
}

shuffled8 simply returns a list of numbers 1 to 8 shuffled. Since you need two of such lists, you invoke it twice, and store it in first and second. You then pair first.get(i) with second.get(i) to get the properties that you want.

To generalize this, if you need triplets, then you just add List<Integer> third = shuffled8();, and then first.get(i), second.get(i), third.get(i) is a triplet that has the properties that you want.

Upvotes: 0

missingfaktor
missingfaktor

Reputation: 92056

The following will do the job I think :

import java.util.*;

class Randoms {
  public static void main(String[] args) {
    List<Integer> randoms = new ArrayList<Integer>();
    Random randomizer = new Random();
    for(int i = 0; i < 8; ) {
      int r = randomizer.nextInt(8) + 1;
      if(!randoms.contains(r)) {
        randoms.add(r);
        ++i;
      }
    }
    List<Integer> clonedList = new ArrayList<Integer>();
    clonedList.addAll(randoms);
    Collections.shuffle(clonedList);

    int[][] cards = new int[8][];
    for(int i = 0; i < 8; ++i) {
      cards[i] = new int[]{ randoms.get(i), clonedList.get(i) };
    }

    for(int i = 0; i < 8; ++i) {
      System.out.println(cards[i][0] + " " + cards[i][1]);
    }
  }
}

One sample run of the above gives :

1 2
8 6
4 3
3 7
2 8
6 1
5 5
7 4

Hope that helps.

Upvotes: 2

Keith Randall
Keith Randall

Reputation: 23265

Generally, if you put the numbers you wish to generate in an array (in your case, an array of length 16 with two each of 1, 2, ..., 8), then randomly permute the array, you will get what you want. You can randomly permute the array using code here.

Upvotes: 1

Related Questions