I have an array that contains the int
values 1, 2, and 3 and I want to randomly assign all of those values to three different variables (without repeating any of the values). This is what I've gotten so far, but when I tested it, it would sometimes duplicate one of the values.
Question: How can I randomly distribute all of the elements of an array to several different variables?
//method
public static double calculate(int randomVal[]){
Random random = new Random();
double randomAnswer = 0;
for(int i = 0;i < randomVal.length; i++){
randomAnswer = (randomVal[random.nextInt(randomVal.length)]);
}
return randomAnswer;
//create array
int[] randomVal = new int[] {1,2,3};
double solution1 = MathGame.calculate(randomVal);
double solution2 = MathGame.calculate(randomVal);
double solution3 = MathGame.calculate(randomVal);