I must prompt for a number range and seed, then create an array of 5 numbers per row that has 2 of each number within the given range, in ASCENDING order. Following this, the order should be shuffled using a seeded randomiser, until all original values have been used.
EDIT (instructions I was given for specifically the last for loop):
This is the last step in my code but I still receive duplicates of collection values.
I have managed to print the array both times succesfully, however the shuffled values don't match the order given in the Zybooks answers. I assume its something with nextInt but I can't figure out what, as I receive duplicates of the same numbers. For those wondering how there can be a set answer, it is because the randomiser is using a seed, meaning the same seed should yield the same results.
import java.util.Random;
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);System.out.println("Please enter the number of elements to be paired.");
System.out.println();
int numPairs = scnr.nextInt();
int[] board = new int[numPairs * 2];
// populate the board
int num = 1;
for (int i = 0; i < board.length; i+= 2) {
board[i] = num;
board[i + 1] = num;
num++;
}
//print initial pairs
for (int i = 0; i < board.length; i++) {
System.out.printf("%4d", board[i]);
if ((i + 1) % 5 == 0 || board.length - 1 == i) {
System.out.println();
}
}
System.out.println();
System.out.println("Please enter a seed number for testing purposes.");
System.out.println();
//SEED a randomiser for testing purposes (use the Random class for this).
int seed = scnr.nextInt();
Random random = new Random();
random.setSeed(seed);
int remainingElements = board.length;
for (int i = 0; i < board.length; i++) {
board[i] = random.nextInt(numPairs) + 1;
if (board[i] != 0) {
System.out.printf("%4d", board[i]);
if ((i + 1) % 5 == 0 || board.length - 1 == i) {
System.out.println();
board[i] = 0;
}
}
}
}
}
I am testing with values 5 and 22, 5 being the number of pairs and 22 being the seed. Expected result is (2 1 1 5 3 4 2 4 3 5), what I receive currently is (4 4 5 3 2 1 3 5 1), again using 5 and 22 as input values. What confuses me most is how I can still receive duplicate numbers despite the if (board[i] != 0)
part..
As has been said in the comments by @Slaw and @Anonymous:
The algorithm that your teacher is after is (partly pseudocode):
// Iterate until all numbers have been used
while (remainingElements > 0) {
// Retrieve a random number to be used as an index
// to take a value from original collection
int i = <random index>;
// If number does not equal zero, print and make it equal to zero
if (board[i] != 0) {
print board[i];
board[i] = 0;
remainingElements--;
}
}
For picking a random index into your board you need to use random.nextInt(board.length)
.
Notice that we only decrease remainingElements
when a number is picked and printed, not every time through the loop. This causes the loop to repeat more times than there are elements in the board
array. Eventually the random index will also pick the last remaining elements, remainingElements
will be decreased to 0 and the loop will terminate.