delphi 生成16位,随机序列,以下代码如何修改,才能每次生成,包含h,k,l三个不同位置的序列!(100)

  • 主题发起人 主题发起人 DYI
  • 开始时间 开始时间
D

DYI

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi 生成16位,随机序列,以下代码如何修改,才能每次生成,包含h,k,l三个不同位置的序列!unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}function RandomString(mChars: string; mLength: Integer): string; var I: Integer; begin Randomize; Result := ''; if mChars = '' then Exit; for I := 1 to mLength do Result := Result + mChars[Random(Length(mChars)) + 1]; end; { RandomString } procedure TForm1.Button1Click(Sender: TObject);beginCaption := RandomString('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', 16);end;end.
 
调用三次就可以了.
 
将产生3次随机数 将作为h,k,l三各得位置
 
type TForm1 = class(TForm) Button1: TButton; function RandomString(mChars:string;mLength:Integer):string; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}{ TForm1 }function TForm1.RandomString(mChars: string; mLength: Integer): string;vari,j,n,m,k:integer;begin randomize; j:=1;while j<3 do begin m:=random(16)+1; n:=random(16)+1; k:=random(16)+1; if (m=n) or (n=k) or (m=k) then continue else j:=3; end; result:=''; if mchars='' then exit; for i:=1 to mlength do begin if i=m then result:=result+'h' else if i=n then result:=result+'k' else if i=k then result:=result+'l' else result:=result+mchars[random(length(mchars))+1]; end;end;procedure TForm1.Button1Click(Sender: TObject);beginCaption:=RandomString('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',16);end;end.试试看,呵呵!
 
接受答案了.
 
后退
顶部