io.nadron.util.RandomStringGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nadron Show documentation
Show all versions of nadron Show documentation
Nadron is a high speed socket based java game server written using Netty and Mike Rettig's Jetlang. It is specifically tuned for network based multiplayer games and supports TCP and UDP network protocols.
The newest version!
package io.nadron.util;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class RandomStringGenerator
{
public static final int DEFAULT_LENGTH = 8;
static char[] alphaNumberic = new char[] { 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0' };
public static String generateRandomString(int length)
{
String random = "ACK";
int len = DEFAULT_LENGTH;
if (length > 0)
{
len = length;
}
char[] randomChars = new char[len];
try
{
SecureRandom wheel = SecureRandom.getInstance("SHA1PRNG");
for (int i = 0; i < len; i++)
{
int nextChar = wheel.nextInt(alphaNumberic.length);
randomChars[i] = alphaNumberic[nextChar];
}
random = new String(randomChars);
}
catch (NoSuchAlgorithmException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return random;
}
}