import java.util.*;Actual random numbers are obtained using nextInt(), and then knocked down to the relevant range using the modulo ("%") operator.
public class Test {
private static Random rn = new Random();
private Test()
{
}
public static int rand(int lo, int hi)
{
int n = hi - lo + 1;
int i = rn.nextInt() % n;
if (i < 0)
i = -i;
return lo + i;
}
public static String randomstring(int lo, int hi)
{
int n = rand(lo, hi);
byte b[] = new byte[n];
for (int i = 0; i < n; i++)
b[i] = (byte)rand('a', 'z');
return new String(b, 0);
}
public static String randomstring()
{
return randomstring(5, 25);
}
}
Thursday, October 30, 2008
generating random characters
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment