Joao Pedro
Joao Pedro

Reputation: 59

Java: Generate Unique Random Character

I have a string of "random" characters. I assigned a numeric value to each character depending on its position in the string and then set a loop to output the character at whatever random position gets chosen. Here's my code so far:

public class Random9_4 {

  public static void main(String[] args) {

    final String chords = "ADE";
    final int N = chords.length();
    java.util.Random rand = new java.util.Random();
    for(int i = 0; i < 50; i++)
    {
        //char s = chords.charAt(rand.nextInt(N));
        //char t = chords.charAt(rand.nextInt(N));

        System.out.println(chords.charAt(rand.nextInt(N)));
        //temp variable
        //while(s == t)
        //{
        //  
        //}System.out.println(chords.charAt(rand.nextInt(N)));
    }
  }
}

As of now it works fine but the characters can repeat at times. I want it so that it is "unique" output of characters (meaning the subsequent character does not repeat). I understand one way to do this is to use a temporary variable to check the current character with the previous one and the character that will be displayed next but I am unsure of how to get started.

Upvotes: 1

Views: 367

Answers (2)

tzatalin
tzatalin

Reputation: 432

I am not sure I understand your question correctly.

Does it just create a variable to store the previous output? Like the bleow code.

    final String chords = "ADE";
    final int N = chords.length();
    Random rand = new Random();
    Character curr = null;
    Character prev = null;
    for (int i = 0; i < 50; i++) {
        curr = chords.charAt(rand.nextInt(N));
        while (curr == prev)
            curr = chords.charAt(rand.nextInt(N));

        prev = curr;
        System.out.println(curr);
    }

Upvotes: 0

Amardeep Bhowmick
Amardeep Bhowmick

Reputation: 16908

You need to use an inner loop to generate a new character if it matches with the character generated in the last iteration.

The temp is a temporary character variable which remembers the last character generated. So in the while loop, we will iterate until a new character has been generated which is not the same as the character in the temp variable.

If a new character is generated, it will be assigned to the temp variable, so on the next iteration the same logic can be applied.

public static void main(String[] args) {
        final String chords = "ADE";
        final int N = chords.length();
        Random rand = new Random();
        char temp = 0;
        for (int i = 0; i < 50; i++) {
           char s = chords.charAt(rand.nextInt(N));
           while(s == temp){ //loop until a new character is generated, this loop will stop when s != temp
               s = chords.charAt(rand.nextInt(N));
           }
           temp = s; //assign current character to the temp variable, so on next iteration this can be compared with the new character generated.
           System.out.println(s);
        }
}

Upvotes: 1

Related Questions