Random code of colors(jawa.awt) without duplicates












-1















So i'm trying to make a random code that uses jawa.awt.colors. I want the code not be able to have duplicates, like it does atm.
The code needs to hold 4 colors.



I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors and i need to use the java.awt.colors later in my code so i can't just use something else.



Is there any other way?



Below is my code that does make duplicates:



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = {Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK,Color.BLACK};

public Color SecretColorCombi = new Color[codeLength];


public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
SecretColorCombi[i] = PossibleColors[random.nextInt(PossibleColors.length)];
}
}

}









share|improve this question




















  • 1





    I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors - sure it does. Post your code showing how you used the method.

    – camickr
    Nov 21 '18 at 20:06
















-1















So i'm trying to make a random code that uses jawa.awt.colors. I want the code not be able to have duplicates, like it does atm.
The code needs to hold 4 colors.



I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors and i need to use the java.awt.colors later in my code so i can't just use something else.



Is there any other way?



Below is my code that does make duplicates:



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = {Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK,Color.BLACK};

public Color SecretColorCombi = new Color[codeLength];


public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
SecretColorCombi[i] = PossibleColors[random.nextInt(PossibleColors.length)];
}
}

}









share|improve this question




















  • 1





    I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors - sure it does. Post your code showing how you used the method.

    – camickr
    Nov 21 '18 at 20:06














-1












-1








-1








So i'm trying to make a random code that uses jawa.awt.colors. I want the code not be able to have duplicates, like it does atm.
The code needs to hold 4 colors.



I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors and i need to use the java.awt.colors later in my code so i can't just use something else.



Is there any other way?



Below is my code that does make duplicates:



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = {Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK,Color.BLACK};

public Color SecretColorCombi = new Color[codeLength];


public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
SecretColorCombi[i] = PossibleColors[random.nextInt(PossibleColors.length)];
}
}

}









share|improve this question
















So i'm trying to make a random code that uses jawa.awt.colors. I want the code not be able to have duplicates, like it does atm.
The code needs to hold 4 colors.



I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors and i need to use the java.awt.colors later in my code so i can't just use something else.



Is there any other way?



Below is my code that does make duplicates:



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = {Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK,Color.BLACK};

public Color SecretColorCombi = new Color[codeLength];


public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
SecretColorCombi[i] = PossibleColors[random.nextInt(PossibleColors.length)];
}
}

}






java random






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:12









Pushpesh Kumar Rajwanshi

11.4k21230




11.4k21230










asked Nov 21 '18 at 19:11









user10687540user10687540

31




31








  • 1





    I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors - sure it does. Post your code showing how you used the method.

    – camickr
    Nov 21 '18 at 20:06














  • 1





    I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors - sure it does. Post your code showing how you used the method.

    – camickr
    Nov 21 '18 at 20:06








1




1





I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors - sure it does. Post your code showing how you used the method.

– camickr
Nov 21 '18 at 20:06





I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors - sure it does. Post your code showing how you used the method.

– camickr
Nov 21 '18 at 20:06












1 Answer
1






active

oldest

votes


















-2














Using Collections.shuffle method you can achieve it using following method,



public static Color possibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public static List<Color> colorList = Arrays.asList(possibleColors);

public static List<Color> getRandomColorList() {
Collections.shuffle(colorList);
return colorList.subList(0, 4);
}


As an alternate, you can also try using following solution and see how to avoid picking duplicate Color object.



As you want to pick the colors randomly but unique, change the strategy of picking elements randomly from a fixed array. Because your random number out of six elements can repeat. To make it repeat immune, have your colors stored in a list rather than an array. You still pick the elements randomly from list, but you remove the color that gets picked once from the list, this way a color that has been picked will never be repeated again, as it is no longer there in the list.



Here is an updated code of your version for my strategy above,



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public List<Color> colorList = new ArrayList<Color>(Arrays.asList(PossibleColors));

public Color SecretColorCombi = new Color[codeLength];

public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
int randIndex = random.nextInt(colorList.size());
Color randColor = colorList.remove(randIndex);
System.out.println("Length: " + colorList.size() + ", Color Picked: " + randColor);
SecretColorCombi[i] = randColor;
}
}

public static void main(String args) {
SecretCombination secretCombination = new SecretCombination();
Stream.of(secretCombination.SecretColorCombi).forEach(System.out::println);
}

}


Finally you can print the colors in the main method to verify that they never repeat.



Here is one of the sample output for the program where you can see the colors are not repeated and the list of colors shrinks by one as it loops.



Length: 5, Color Picked: java.awt.Color[r=255,g=175,b=175]
Length: 4, Color Picked: java.awt.Color[r=255,g=0,b=0]
Length: 3, Color Picked: java.awt.Color[r=0,g=0,b=255]
Length: 2, Color Picked: java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=175,b=175]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=0,g=0,b=255]
java.awt.Color[r=0,g=0,b=0]





share|improve this answer





















  • 1





    (1-) Why reinvent the wheel when Collections.shuffle(...) will work?

    – camickr
    Nov 21 '18 at 21:10













  • Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:01











  • He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

    – camickr
    Nov 22 '18 at 14:51














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419031%2frandom-code-of-colorsjawa-awt-without-duplicates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-2














Using Collections.shuffle method you can achieve it using following method,



public static Color possibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public static List<Color> colorList = Arrays.asList(possibleColors);

public static List<Color> getRandomColorList() {
Collections.shuffle(colorList);
return colorList.subList(0, 4);
}


As an alternate, you can also try using following solution and see how to avoid picking duplicate Color object.



As you want to pick the colors randomly but unique, change the strategy of picking elements randomly from a fixed array. Because your random number out of six elements can repeat. To make it repeat immune, have your colors stored in a list rather than an array. You still pick the elements randomly from list, but you remove the color that gets picked once from the list, this way a color that has been picked will never be repeated again, as it is no longer there in the list.



Here is an updated code of your version for my strategy above,



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public List<Color> colorList = new ArrayList<Color>(Arrays.asList(PossibleColors));

public Color SecretColorCombi = new Color[codeLength];

public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
int randIndex = random.nextInt(colorList.size());
Color randColor = colorList.remove(randIndex);
System.out.println("Length: " + colorList.size() + ", Color Picked: " + randColor);
SecretColorCombi[i] = randColor;
}
}

public static void main(String args) {
SecretCombination secretCombination = new SecretCombination();
Stream.of(secretCombination.SecretColorCombi).forEach(System.out::println);
}

}


Finally you can print the colors in the main method to verify that they never repeat.



Here is one of the sample output for the program where you can see the colors are not repeated and the list of colors shrinks by one as it loops.



Length: 5, Color Picked: java.awt.Color[r=255,g=175,b=175]
Length: 4, Color Picked: java.awt.Color[r=255,g=0,b=0]
Length: 3, Color Picked: java.awt.Color[r=0,g=0,b=255]
Length: 2, Color Picked: java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=175,b=175]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=0,g=0,b=255]
java.awt.Color[r=0,g=0,b=0]





share|improve this answer





















  • 1





    (1-) Why reinvent the wheel when Collections.shuffle(...) will work?

    – camickr
    Nov 21 '18 at 21:10













  • Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:01











  • He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

    – camickr
    Nov 22 '18 at 14:51


















-2














Using Collections.shuffle method you can achieve it using following method,



public static Color possibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public static List<Color> colorList = Arrays.asList(possibleColors);

public static List<Color> getRandomColorList() {
Collections.shuffle(colorList);
return colorList.subList(0, 4);
}


As an alternate, you can also try using following solution and see how to avoid picking duplicate Color object.



As you want to pick the colors randomly but unique, change the strategy of picking elements randomly from a fixed array. Because your random number out of six elements can repeat. To make it repeat immune, have your colors stored in a list rather than an array. You still pick the elements randomly from list, but you remove the color that gets picked once from the list, this way a color that has been picked will never be repeated again, as it is no longer there in the list.



Here is an updated code of your version for my strategy above,



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public List<Color> colorList = new ArrayList<Color>(Arrays.asList(PossibleColors));

public Color SecretColorCombi = new Color[codeLength];

public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
int randIndex = random.nextInt(colorList.size());
Color randColor = colorList.remove(randIndex);
System.out.println("Length: " + colorList.size() + ", Color Picked: " + randColor);
SecretColorCombi[i] = randColor;
}
}

public static void main(String args) {
SecretCombination secretCombination = new SecretCombination();
Stream.of(secretCombination.SecretColorCombi).forEach(System.out::println);
}

}


Finally you can print the colors in the main method to verify that they never repeat.



Here is one of the sample output for the program where you can see the colors are not repeated and the list of colors shrinks by one as it loops.



Length: 5, Color Picked: java.awt.Color[r=255,g=175,b=175]
Length: 4, Color Picked: java.awt.Color[r=255,g=0,b=0]
Length: 3, Color Picked: java.awt.Color[r=0,g=0,b=255]
Length: 2, Color Picked: java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=175,b=175]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=0,g=0,b=255]
java.awt.Color[r=0,g=0,b=0]





share|improve this answer





















  • 1





    (1-) Why reinvent the wheel when Collections.shuffle(...) will work?

    – camickr
    Nov 21 '18 at 21:10













  • Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:01











  • He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

    – camickr
    Nov 22 '18 at 14:51
















-2












-2








-2







Using Collections.shuffle method you can achieve it using following method,



public static Color possibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public static List<Color> colorList = Arrays.asList(possibleColors);

public static List<Color> getRandomColorList() {
Collections.shuffle(colorList);
return colorList.subList(0, 4);
}


As an alternate, you can also try using following solution and see how to avoid picking duplicate Color object.



As you want to pick the colors randomly but unique, change the strategy of picking elements randomly from a fixed array. Because your random number out of six elements can repeat. To make it repeat immune, have your colors stored in a list rather than an array. You still pick the elements randomly from list, but you remove the color that gets picked once from the list, this way a color that has been picked will never be repeated again, as it is no longer there in the list.



Here is an updated code of your version for my strategy above,



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public List<Color> colorList = new ArrayList<Color>(Arrays.asList(PossibleColors));

public Color SecretColorCombi = new Color[codeLength];

public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
int randIndex = random.nextInt(colorList.size());
Color randColor = colorList.remove(randIndex);
System.out.println("Length: " + colorList.size() + ", Color Picked: " + randColor);
SecretColorCombi[i] = randColor;
}
}

public static void main(String args) {
SecretCombination secretCombination = new SecretCombination();
Stream.of(secretCombination.SecretColorCombi).forEach(System.out::println);
}

}


Finally you can print the colors in the main method to verify that they never repeat.



Here is one of the sample output for the program where you can see the colors are not repeated and the list of colors shrinks by one as it loops.



Length: 5, Color Picked: java.awt.Color[r=255,g=175,b=175]
Length: 4, Color Picked: java.awt.Color[r=255,g=0,b=0]
Length: 3, Color Picked: java.awt.Color[r=0,g=0,b=255]
Length: 2, Color Picked: java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=175,b=175]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=0,g=0,b=255]
java.awt.Color[r=0,g=0,b=0]





share|improve this answer















Using Collections.shuffle method you can achieve it using following method,



public static Color possibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public static List<Color> colorList = Arrays.asList(possibleColors);

public static List<Color> getRandomColorList() {
Collections.shuffle(colorList);
return colorList.subList(0, 4);
}


As an alternate, you can also try using following solution and see how to avoid picking duplicate Color object.



As you want to pick the colors randomly but unique, change the strategy of picking elements randomly from a fixed array. Because your random number out of six elements can repeat. To make it repeat immune, have your colors stored in a list rather than an array. You still pick the elements randomly from list, but you remove the color that gets picked once from the list, this way a color that has been picked will never be repeated again, as it is no longer there in the list.



Here is an updated code of your version for my strategy above,



public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color PossibleColors = { Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK, Color.BLACK };
public List<Color> colorList = new ArrayList<Color>(Arrays.asList(PossibleColors));

public Color SecretColorCombi = new Color[codeLength];

public SecretCombination() {
for (int i = 0; i < codeLength; i++) {
int randIndex = random.nextInt(colorList.size());
Color randColor = colorList.remove(randIndex);
System.out.println("Length: " + colorList.size() + ", Color Picked: " + randColor);
SecretColorCombi[i] = randColor;
}
}

public static void main(String args) {
SecretCombination secretCombination = new SecretCombination();
Stream.of(secretCombination.SecretColorCombi).forEach(System.out::println);
}

}


Finally you can print the colors in the main method to verify that they never repeat.



Here is one of the sample output for the program where you can see the colors are not repeated and the list of colors shrinks by one as it loops.



Length: 5, Color Picked: java.awt.Color[r=255,g=175,b=175]
Length: 4, Color Picked: java.awt.Color[r=255,g=0,b=0]
Length: 3, Color Picked: java.awt.Color[r=0,g=0,b=255]
Length: 2, Color Picked: java.awt.Color[r=0,g=0,b=0]
java.awt.Color[r=255,g=175,b=175]
java.awt.Color[r=255,g=0,b=0]
java.awt.Color[r=0,g=0,b=255]
java.awt.Color[r=0,g=0,b=0]






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 9:06

























answered Nov 21 '18 at 19:36









Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

11.4k21230




11.4k21230








  • 1





    (1-) Why reinvent the wheel when Collections.shuffle(...) will work?

    – camickr
    Nov 21 '18 at 21:10













  • Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:01











  • He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

    – camickr
    Nov 22 '18 at 14:51
















  • 1





    (1-) Why reinvent the wheel when Collections.shuffle(...) will work?

    – camickr
    Nov 21 '18 at 21:10













  • Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

    – Pushpesh Kumar Rajwanshi
    Nov 22 '18 at 9:01











  • He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

    – camickr
    Nov 22 '18 at 14:51










1




1





(1-) Why reinvent the wheel when Collections.shuffle(...) will work?

– camickr
Nov 21 '18 at 21:10







(1-) Why reinvent the wheel when Collections.shuffle(...) will work?

– camickr
Nov 21 '18 at 21:10















Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

– Pushpesh Kumar Rajwanshi
Nov 22 '18 at 9:01





Yes Collections.shuffle surely works but my answer was for this statement from OP Is there any other way? Moreover, my answer was also to show how OP could correct his strategy, which shouldn't be a bad thing that really deserved a downvote. Downvoting an incorrect answer is understandable but downvoting an alternate approach doesn't seem fine. With my answer OP at least got some insight, that in case he has to implement something similar, he knows how to do it. I've also added a solution using shuffle method.

– Pushpesh Kumar Rajwanshi
Nov 22 '18 at 9:01













He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

– camickr
Nov 22 '18 at 14:51







He asked for another way because he couldn't get Collections.shuffle() to work. The problem is NOT with Collections.shuffle(). The problem is with the OP's code. The OP should learn to read the API and use existing methods. If there is a problem we should help him understand the usage of the method. The down vote was to discourage anybody else from using the solution when there is a simple one line solution already available in the API. People always seem to pay more attention to a down vote then just a comment.

– camickr
Nov 22 '18 at 14:51






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419031%2frandom-code-of-colorsjawa-awt-without-duplicates%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word