Title: Random string generator
Question: Ever wanted to generate a random string with specific random char on a specific position in the string?
Answer:
I needed a random string generator that would make me a string with a random char on a specific position in the string that I would specify the expression from.
I.e. I wanted the string to be 6 chars long, the first three chars to contain only A..Z and a..z chars and the last three chars to contain only 0..9 chars.
So I made this function, it's simple and fast.
You pass the parameter for which chars to include in the random state.
-------------------- snip ---------------------
function RandomString(expr: string): string;
{
(c) 2002 Uros Gaber, PowerCom d.o.o.
expr values that combine (*nix style)
1: A..Z
2: a..z
4: 0..9
if you want
(A..Z, a..z) use 3;
(A..Z, a..z, 0..9) use 7
(A..Z, 0..9) use 5
(a..z, 0..9) use 6
i.e. RandomString('123'); to generate a 3 letters random string...
}
var
i: Byte;
s: string;
v: Byte;
begin
Randomize;
SetLength(Result, Length(expr));
for i:=1 to Length(expr) do
begin
s:='';
try
v:=StrToInt(Expr[i]);
except
v:=0;
end;
if (v-4) = 0 then
begin
s:=s+'0123456789';
dec(v, 4);
end;
if (v-2) = 0 then
begin
s:=s+'abcdefghijklmnopqrstuvwxyz';
dec(v, 2);
end;
if (v-1) = 0 then
s:=s+'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
Result[i]:=s[Random(Length(s)-1)+1];
end;
end;
-------------------- snip ---------------------