Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Length String of Random Characters

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
I feel certain I saw the answer to this one on this forum sometime in the last week or two but can't re-find it for looking.
I'm looking to have a simple function that returns a string of random characters, the string itself being of a random length.
Any thoughts anyone?
Thanks in advance.
Steve
 
Hi,
Hope this helps

function Randomstring(maxlength:integer):string;
var i,j:integer;
begin
i:=random(maxlength);

for x:=1 to i do
result:=result+char(random(255));

end;
 
By the way, it became a really funny program !!!
Having lots of fun with it :) [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
A simpler version is something like this btw:

function RanStr(MaxNumberOfChars: Integer): String;
var MaxLength, i: Integer;
begin
i := 0;
Randomize;
MaxLength := Random(MaxNumberOfChars);

while i < MaxLength do
begin
i := i + 1;
Result := Result + Chr(Ord('A') + Random(26));
end;
end;

It needs to the second random needs to be 26 in stead of 25
as MikeEd posted in my question, because it won't generate
a Z if it's 25.

Good luck, [bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top