I would pick a random number from 0-255 and then compare it against a Pattern instantiated with a regular expression that defines the characters you are interested in (you can use deletions to get rid of the six characters you don't want). Then just pick a length randomly and do that for each character.
Not a terribly efficient solution, but I assume that you are not generating huge numbers of these things at one go.
By the way, you should only use data randmoly generated from java for light security. Computers cannot actually generate random numbers without special hardware, so your security will be vulnerable if you go this route.
Drake
> I have a unique question.
I believe it is a quite common question.
> I need to generate a unique random Alpha Num string.
> I cannot use 0,5,8 and 0,S,B in the generated output string.
> The string generated should be no more than length 7 max.
Fill a char array with the characters you allow and randomly select from that array.
static final char[] allow = "ACDEFGHIJKLMNPQRTUVWXYZ1234679".toCharArray();
static final Random pseudo = new Random();
(...)
// loop
char nextChar = allow[pseudo.nextInt(allow.length)];
// end-loop
The rest should be easy.
Please note that your tutor may very well be reading these forums.
Following will generate random unique combinations of a given array.
It would become even better if it was possible to generate a random
one-to-one and onto map between all long numbers.
Does anyone know of a way to create a random bijection of all long
numbers? http://mathworld.wolfram.com/Bijection.html
And the code.
import java.util.*;
public class UniqueStringGenerator {
public static String[] ALPHA_NUM =
{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","X","Y","Z",
"0","1","2","3","4","5","6","7","8","9"};
private List _seed = null;
private long _next;
private int _length;
public UniqueStringGenerator(String[] parts, int length) {
_length = length;
_seed = new Vector();
// setting seed
List all = new Vector();
for (int i = 0;i<parts.length;i++) {
all.add(parts[i]);
}
while (all.size()>0) {
int p = (int)(((double)all.size())*Math.random());
_seed.add(all.get(p));
all.remove(p);
}
// setting first current which start with length number of ones
int p = 1;
_next = p;
for (int i=0;i<_length-1;i++) {
p = p*2;
_next = _next + p;
}
}
public String nextUnique() {
StringBuffer sb = new StringBuffer();
long p = 1;
for (int i = 0;i<_seed.size();i++) {
if ( (_next & p) > 0) {
sb.append(_seed.get(i));
}
p = p*2;
}
_next = nextNumberWithSameNumberOfBits(_next);
return sb.toString();
}
// this function will generate next long with _length number of bits.
private long nextNumberWithSameNumberOfBits(long a){
long _length = a & -a;
long r = a + _length;
a = (((r^a) >>> 2) / _length) | r;
return a;
}
public static void main(String[] args) {
int length = Integer.parseInt(args[0]);
int num = Integer.parseInt(args[1]);
UniqueStringGenerator usg = new UniqueStringGenerator
(UniqueStringGenerator.ALPHA_NUM, length);
for (int i=0;i<num;i++) {
System.out.println(usg.nextUnique());
}
}
}
>
Following solution generates random unique strings combinations
from any given base set. Here is a sample output.
$ java UniqueStringGenerator 7 100 10
FCT2RYT
2M31BU8
VV8FU4Z
BON2MKE
OKUJZT0
68E8X8S
YTNLZAY
CPEKPFP
C176M24
GQB9QE9
The idea is quite simple.
Pre-work:
1. The base set is first mixed to increase entropy.
2. All possible combinations are divide up into random buckets.
Work for every unique string:
1. Pick a bucket at random.
2. Get next number in bucket.
3. Convert the number to a unique string token.
It is possible to adjust how much entropy you want in your
sequence of generated combinations by choosing how many buckets
the generator contains. However, every bucket will take up some
memory.
import java.util.*;
import java.math.*;
public class UniqueStringGenerator {
public static String[] ALPHA_NUM =
{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P",
"Q","R","S","T","U","V","X","Y","Z",
"0","1","2","3","4","5","6","7","8","9"};
private List _parts = null;
private List _buckets = null;
private int _length;
private BigInteger _range = null;
public UniqueStringGenerator(String[] parts, int length,
int numberOfBuckets) {
_length = length;
// mixing the base set to increase entropy
_parts = new Vector();
List all = new Vector();
for (int i = 0;i < parts.length;i++) {
all.add(parts[i]);
}
while (all.size()>0) {
int p = (int)(((double)all.size())*Math.random());
_parts.add(all.get(p));
all.remove(p);
}
// dividing all possible numbers into given number of buckets
BigInteger numParts = BigInteger.valueOf(parts.length);
_range = BigInteger.ONE;
for (int i = 0;i<_length;i++) {
_range = _range.multiply(numParts);
}
TreeSet starts = new TreeSet();
Random rand = new Random();
while (starts.size()<numberOfBuckets-1) {
BigInteger bi = new BigInteger(_range.bitLength(), rand);
if (bi.compareTo(_range)><=0) {
starts.add(bi);
}
}
_buckets = new Vector();
Bucket last = new Bucket(BigInteger.ZERO);
_buckets.add(last);
for (Iterator i = starts.iterator();i.hasNext();) {
Bucket next = new Bucket((BigInteger)i.next());
_buckets.add(next);
last.setTo(next.getStart());
last = next;
}
last.setTo(_range);
}
private class Bucket {
private BigInteger _start;
private BigInteger _next;
private BigInteger _to;
public Bucket(BigInteger start) {
_start = start;
_next = start;
}
public void setTo(BigInteger to) {
_to = to;
}
public BigInteger getStart() {
return _start;
}
public boolean hasNext() {
return _next.compareTo(_to)<0;
}
public BigInteger next() {
BigInteger ret = _next;
_next = _next.add(BigInteger.ONE);
return ret;
}
public String toString() {
return "["+_start+", "+_next+", "+_to+"]:"+hasNext();
}
}
public String nextUnique() {
// getting next big integer
BigInteger left = null;
while (left==null) {
Bucket b = (Bucket)
_buckets.get((int)(Math.random()*_buckets.size()));
if (b.hasNext()) {
left = b.next();
}
}
// converting it to a combination
StringBuffer unique = new StringBuffer();
BigInteger base = BigInteger.valueOf(_parts.size());
for (int i = 0;i<_length;i++) {
BigInteger[] resRem = left.divideAndRemainder(base);
left = resRem[0];
unique.append(_parts.get(resRem[1].intValue()));
}
return unique.toString();
}
public static void main(String[] args) {
if (args.length!=3) {
System.out.println("Usage: UniqueStringGenerator "+
"[string size] [# buckets] [# prints]");
return;
}
int length = Integer.parseInt(args[0]);
int numBuckets = Integer.parseInt(args[1]);
int num = Integer.parseInt(args[2]);
UniqueStringGenerator usg = new UniqueStringGenerator
(UniqueStringGenerator.ALPHA_NUM, length, numBuckets);
for (int i=0;i<num;i++) {
System.out.println(usg.nextUnique());
}
}
}
>