Saturday, May 9, 2009

Random Number generator

public static String generateLongNumber(int digit)
{
Random random = new Random();
String convert = "0";

BigInteger bigGen;
BigInteger bigComp = BigInteger.valueOf(-1L);
Long generated = random.nextLong();

bigGen = BigInteger.valueOf(generated);
convert = String.valueOf(generated);

if(generated < 0) {
convert = String.valueOf(bigGen.multiply(bigComp));
}
if((convert.length() > digit )) {
convert = convert.substring(0,digit);
generated = Long.parseLong(convert);
} else if((convert.length() < digit )) {
generateLongNumber(digit);
}
return generated.toString();
}

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails