4

is there any way to pick a random string from an array, and not pick the string again. once all the strings are picked only once, the program ends.

here is my current system, public static void main (String [] argv) {

    //String [] names = new String[5];
    String [] names = {"Snoopy", "Donald Duck", "Mickey Mouse", "Little Timmy", "Spiderman"};
    //put a list of names in an array
    //randomly pick a name out of the array
    //how to: randomly pick a number as the index
    //range: 0 to the number of names - 1


    int n = -1;
    int previous = n;
    int storing1 = -1;
    int storing2 = -1;
    int storing3 = -1;
    int storing4 = -1;

    System.out.println(previous);
    while (true) {

        System.out.println("Hit \"enter\" to spin");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        //if (s.equalsIgnoreCase("s")) {

        storing4 = storing3;
        storing3 = storing2;
        storing2 = storing1;
        storing1 = previous;
        previous = n;
        n = (int) (Math.random() * names.length);

        if (n == previous || n == storing1 || n == storing2 || n == storing3 || n == storing4) {
            System.out.println("This person has already been picked");

            break;

        }

        System.out.print("n = " + n + " ");
        String picked = names[n];
        System.out.println("Congratulations, " + picked + " was picked!");

        }
        System.out.println(previous);
        System.out.println(storing1);
        System.out.println(storing2);
        System.out.println(storing3);
        System.out.println(storing4); //for my own use, to tell if the storing system works properly



        }

my problem is that the console will return "this person has already been picked" and on and on. not to say that the program ending system doesn't work since the break will work prematurely.

[1]: https://i.sstatic.net/54Eef.png [as you can see, the program ends even though not all five names has been picked because it has picked the same one before][1] sorry if this seems simple, I just started java a few months ago. i don't think I am allowed to use ArrayList and that's been the only solution to this I've found so far. thank you.

1
  • 1
    make use of Fisher–Yates shuffle Algorithm Commented Jun 16, 2020 at 3:44

3 Answers 3

1

If you cannot use a list with shuffle to store the random names, then perhaps you can use a list of indices and shuffle that instead:

String [] names = {"Snoopy", "Donald Duck", "Mickey Mouse", "Little Timmy", "Spiderman"};
List<Integer> indices = IntStream.of(0,1,2,3,4).boxed().collect(Collectors.toList());
Collections.shuffle(indices);

// now iterate the indices and consume random names, one time only
for (int index : indices) {
    System.out.println("Random name: " + names[index]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I meant that I don't think I can use any imported utilities since my teacher hasn't taught us most of them. Can you think of anything that would work without using the added utilities?
Well, you would need to build something which mimics a Java collection; doubt such an answer would be of much use to anyone else, so I am disinclined to edit.
1

This question could be solved with Random.

You can randomly shuffle this array {names}, and then pick names[index] (index starts from 0, ends with names.length - 1).

How to shuffle this array:

Random r = new Random();
for (int i = 0; i < names.length; i++){
    int nextSwapIndex = r.nextInt(names.length);
    String temp = names[nextSwapIndex];
    names[nextSwapIndex] = names[i];
    names[i] = temp;
}

Comments

0

Just randomize the order of the list and then print out the strings one by one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.