Removing random repeated guesses from brute force program I came here to ask if there is a way to prevent already guessed guesses from being guessed again in my brute-force Java program. Basically I used a rand.nextInt in my program but the program will guess already guessed passwords again even if they were incorrect. How could I prevent this as it wastes time and processing power to guess already guessed numbers?

import java.util.*;
import java.io.*;
class pwcracker
{
public static void main (String[] args)
{
    Scanner scan = new Scanner( System.in );
    Random rand = new Random();



  String pw, choices, guess;
    long tries;
    int j, length;

    System.out.println("Enter a password that is up to 5 chars and contains no numbers: ");
    pw = "" + scan.nextLine();
    length = pw.length();

    choices = "abcdefghijklmnopqrstuvwxyz";
    tries = 0;
    guess = "";

    System.out.println("Your pw is: " + pw);
    System.out.println("The length of your pw is: " + length);

  System.out.println("for TEST- Guess: " + guess + "pw :"+pw);


  if (!guess.equals(pw)){

  while  (!guess.equals(pw)) 
    { 
        j = 0;
        guess = "";

        while ( j &lt; length )
        {
            guess = guess + choices.charAt( rand.nextInt( choices.length() ) ); 
        j = j + 1;

        if (guess .equals(pw))
     {
       System.out.println("Match found, ending loop..");
       break;
     }

        }
                System.out.println("Guess: " + guess + " pw :"+pw);     
        tries = tries + 1;                      
    }
  }
    System.out.println("Here is your password: " + guess);
    System.out.println("It took " + tries + " tries to guess it.");
   }
}
I think that your problem is the use of randomisation - it's not really a brute-force attack if you are guessing.

If it were a 4-digit combination lock you would start at 0000 and then try 0001 and 0002 and so on. That's the brute-force approach. I suggest that you employ exactly the same approach with your password cracker. It should be the same algorithm, but rather than 10 posibilities in each slot you have 26.