-1

What I'm trying to do is create a number of random numbers to be used for passwords. The format I'm trying to achieve is some like 3794093 Y. I have code to do this already but it involves creating a random int and a random char and combining the two. This would be alright but I would like the program to ask the user to enter their code. Should they enter their code and it matches it lets them access the program.

What I'm look for is a way to turn the random number and char into one string or something similar.

Here is code I currently have. I don't want the code done for me as I'd like to try and do it myself, just need advice on where I'm going wrong/what I'm missing.

Random r = new Random();

String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

for (int i = 0; i < 50; i++) {
    int ppsn[]=new int[50]; //Array for student number
    ppsn[i] = 3700000 + (int)(Math.random() * ((3800000 - 3700000) + 1));
    System.out.println(ppsn[i]+" " +alphabet.charAt(r.nextInt(alphabet.length())));
}
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
user2965503
  • 13
  • 1
  • 5

1 Answers1

0

What I'm look for is a way to turn the random number and char into one string or something similar.

Simple enough. Pass the values into a new String object constructor.

String code = new String(ppsn[i]+" " +alphabet.charAt(r.nextInt(alphabet.length())));

How I have done this in the past

I faced this issue before, and I decided to use hashing algorithms to help me generate my pass codes. I had to abandon a strict format, but the truncated message digest of a random number provided a broad and varied distribution.

Firstly, you can find out how to hash a value using this question. You can then truncate it to a more manageable size, say 16 characters..

byte[] digest; // Assume it is filled with output of hashing algorithm

String passcode = new String(digest).substring(0, 16);
Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89