admin管理员组文章数量:1401673
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):
- SEED A RANDOMISER
- ITERATE OVER THE COLLECTION UNTIL ALL NUMBERS ARE USED
- RETRIEVE A RANDOM NUMBER TO BE USED AS AN INDEX TO TAKE A VALUE FROM ORIGINAL COLLECTION
- IF NUMBER DOES NOT EQUAL ZERO PRINT AND MAKE IT EQUAL ZERO
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..
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):
- SEED A RANDOMISER
- ITERATE OVER THE COLLECTION UNTIL ALL NUMBERS ARE USED
- RETRIEVE A RANDOM NUMBER TO BE USED AS AN INDEX TO TAKE A VALUE FROM ORIGINAL COLLECTION
- IF NUMBER DOES NOT EQUAL ZERO PRINT AND MAKE IT EQUAL ZERO
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..
1 Answer
Reset to default 1As 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.
本文标签: javaHow can I shuffle an array using a seeded randomiserStack Overflow
版权声明:本文标题:java - How can I shuffle an array using a seeded randomiser? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744287046a2598930.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
board[i] = random.nextInt(numPairs) + 1
correct? Seems to me you should be doing something along the lines of randomizing the indexes, not the values. – Slaw Commented Mar 23 at 15:53random.nextInt(board.length)
instead ofrandom.nextInt(numPairs+1)
, (2) You subtract 1 fromremainingElements
no matter if you pick and print an element or not, causing too few numbers to be printed. You should only subtract 1 when a number is printed (causing the loop to run more times than there are elements, but it will terminate after all element have been printed, so it’s fine). – Anonymous Commented Mar 25 at 6:58