In the Code below i have a program using multi dimensional arrays. I have a quick question on "How to" ad a checking system so my random class does not generate the same "type" of horse twice. For instance right now i have my program pick a random color with the name stallion i do not want it to pick blue twice

Main Class

String[][] horses = {{"Black ","White ","Gray ","Old ","Young ",
                             "Pink ","Yellow ","Brown ","Orange ","Cyan ", 
                             "Purple ","Tan ","Cerulean ","Silver ","Gold ", 
                             "Red ","Green ","Blue ","Maroon ","Marigold ", 
                             "Iron ","Paper ","Wood ","Zinc ","Metal "},
      String[][] horsePlace = {{pickRand.get(horses[0]), pickRand.get(horses[0]),  pickRand.get(horses[0])
                              , pickRand.get(horses[0]), pickRand.get(horses[0]), pickRand.get(horses[0])},
                              {pickRand.get(horses[1]), pickRand.get(horses[1]), pickRand.get(horses[1]),
                               pickRand.get(horses[1]), pickRand.get(horses[1])}};

Random Class

import java.util.Random;
class pickRand {
    public static String get (String[] array) {
        Random generator = new Random();
        int rnd = generator.nextInt(array.length);
        return array[rnd];

Edited by Eragah: Minor Edit

6 Years
Discussion Span
Last Post by stultuske

keep an array, or a list with types you've already added.
when you create a new one, check in that array or list whether it already exists, if not, add it to the list, if it does, ignore it and create a new one.

for future reference, when you say: this is my main method, you may want to show the complete code you have in the main method, that might be easier for us.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.