0

I have an array e.g. of length 10:

Population = {1,3,4,2,7,-2,0,8,9,5}.

I generate two random numbers between 0-9 (e.g. 4 and 6). So then I check the 4th and 6th elements (7, 0) and the "winner" is the number that is greater. So 7 goes into the winners array (5 winners in total):

Winners = {7, ...}

Each time I want to pair up two of the remaining numbers (including the losers) and put the winner into the winners array.

The easiest way to do this would be to use a non-fixed size array of size n and remove a winner from 'population' and put it into 'winners'. Then I could generate two random numbers between 1 and n-1 and continue the process.

How could I do this using a fixed-size array in Java? How could I select two numbers from my array, ignoring any numbers that are 'winners'?

TheFaster
  • 243
  • 4
  • 18
mmgro27
  • 475
  • 1
  • 8
  • 18
  • Potentially hacky, but what if you used an array of `Integer[]` and set visited values to `null`? – Rogue Feb 04 '15 at 17:00

4 Answers4

1

If you want to use fixed size array then every time you pick the winner (lets say ith position) you will have to iterate over the array and move all elements with index j=i+1 to j and then generate next random numbers between 0 to n=n-1.

Though I would suggest easy data structure to use here would be ArrayList. You can dynamically remove elements from it and use its size() as upper cap to compute random numbers.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

You could determine the winner and then swap it to the back of the array:

int[] population = {1,3,4,2,7,-2,0,8,9,5};
private void turnament() {
    for (int i = population.length - 1; i >= population.length / 2; i--) {
        //you might want to make sure m != n.
        int m = (int) Math.round(Math.random() * i);
        int n = (int) Math.round(Math.random() * i);
        int temp = population[i];
        if (population[m] > population[n]) {
            population[i] = population[m];
            population[m] = temp;
        } else {
            population[i] = population[n];
            population[n] = temp;
        }
    }
}

This will do population.length / 2 tournaments and write the winner to the back, so the last element will be the first winner, the second last the second winner etc.

Or as @Aniket Thakur mentions, the ArrayList would be a fine data structure.

Affe
  • 53
  • 1
  • 1
  • 7
0

Keep an index pointing where is the start of your winners array (assuming you reuse the array). Initially, it will be n. Then get 2 random numbers with Random.nextInt(startOfWinners), swap the winner with n-1 and iterate.

Luis
  • 1,294
  • 7
  • 9
0

Using a standard array, you can implement a sort of randomized queue.

Basically, your queue is the population array; when you choose two opponents, you set their indexes to null and move the nulls at the end of the array, while decrementing available size. Some pseudo-code to clarify:

1 population = [...]
2 winners = [all null here, size probably half of population.length + 1]
3 size = popoluation.length
4 opponents = nextTwoRandoms(size)
5 population[opponents.0] = null
6 population[opponents.1] = null
7 winners = addWinnerBetween(opponents)
8 population = moveNullAtTheEnd(population)
9 size = size - 2
10 repeat from step#4 until satisfied
ThanksForAllTheFish
  • 7,101
  • 5
  • 35
  • 54