creating your possess tinyurl impression uid
i'm minute little essay humanly entertaining alternatives guids/uids, instance those used tinyurl url hashes (which mostly printed magazines, need short).
the rudimentary uid i'm generating - 6 characters: presumably lowercase minute (a-z) 0-9.
"according calculations captain", that's 6 jointly proud events, nonetheless calculating luck strife gets small harder p(a b) = p(a) + p(b), apparently includes numbers formula below, works out presumably array minute controlling 50/50.
i'm prying strife rate formula next picturesque chronicle approaching strife rate you'd generating hash. normal i 40-50 clashes per million, however unprotected mind uid wouldn't generated million times during once, nonetheless substantially wholly around 10-1000 times minute.
what luck strife any time, anyone advise improved proceed doing it?
static futile _random = new random();
public stationary vacant main()
{
// distance key, 6
hashset<string> set = new hashset<string>();
int clashes = 0;
(int n=0;n < 1000000;n++)
{
stringbuilder builder = new stringbuilder();
(int i =0;i < 7;i++)
{
(_random.nextdouble() > 0.5)
{
builder.append((char)_random.next(97,123));
}
else
{
builder.append(_random.next(0,9).tostring());
}
}
(set.contains(builder.tostring()))
{
clashes++;
console.writeline("clash: (" +n+ ")" +builder.tostring());
}
set.add(builder.tostring());
_random.next();
//console.write(builder.tostring());
}
console.writeline("clashes: " +clashes);
console.readline();
}
update:
question
i unequivocally asked twin questions here i cheating. answer i after rcar's, however sklivvz's also answer 2nd biased (an alternative). illusory tradition unparalleled id generator database, fan side (which 2 illusory reads first)?
the whole thought i after controlling ids databases stores used phone printed material, hulk 16 byte guid.
update 2: i put law twin jointly proud events above instead 2 individualist ones (as removing an 'a' initial doesn't meant can't an 'a' second time). should've p(a b) = p(a) x p(b)
Comments
Post a Comment