mercredi 7 octobre 2015

How to get same string generated by pseudo random generator with some initialized string?

How to get same string generated by pseudo random generator with some initialized string in java. I want something like this

I get this code from somewhere. It produces fine random string but what I want is that whenever I pass some string let suppose "Name" with it, it gives me the same random string generated with this function. Means I want same random string with "Name".

public static String RandomAlphaNumericString(int numChars)
{
    char[] All = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456879".toCharArray();
    SecureRandom srand = new SecureRandom();
    Random rand = new Random();
    char[] buff = new char[numChars];
    for (int i = 0; i < numChars; ++i) {
        // reseed rand once you've used up all available entropy bits
        if ((i % 10) == 0) {
          rand.setSeed(srand.nextLong()); // 64 bits of random!
        }
        buff[i] = All[rand.nextInt(All.length)];
    }
    return new String(buff);
}




Aucun commentaire:

Enregistrer un commentaire