Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
498 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (4)


Random string generatorGo to Uros Gaber's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Generate a random string with expression
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
11/02/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet string random expression generate
Times Scored:
6
Visits:
5536
Uploader: Uros Gaber
Company: PowerCom d.o.o.
Reference: PowerCom d.o.o.
 
Question/Problem/Abstract:
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 ---------------------





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
?
    tom chaney (Apr 2 2008 10:27AM)

that looks very useful but where do you paste this - do you use a VCL form application? - i can't get it to work - plz tell me where u paste this code
Respond

RE: ?
Uros Gaber (Apr 2 2008 11:02AM)

You just paste it in your unit and call it. If you have a form with one button and a edit box.

Example:
---------------
unit test;
...
...
...

implementation

procedure Random...
...
...
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text:=RandomString('777777');
end;

end.
Respond

RE: RE: ?
tom chaney (Apr 3 2008 10:45AM)

what does that mean?
Respond

RE: RE: RE: ?
tom chaney (Apr 3 2008 10:54AM)

thanks - i just worked it out

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)