随机生成一个10位的数字(来自大富翁)
1 var
2 Form1: TForm1;
3 Len10NumberList:TStringList;
4
5 implementation
6
7 {$R *.dfm}
8
9 procedure TForm1.FormCreate(Sender: TObject);
10 begin
11 Len10NumberList:=TStringList.Create;
12 Randomize;
13 end;
14
15 function GenerateLen10Number:Int64;
16 var
17 rn1, rn2, rn3 : Int64;
18 begin
19 repeat
20 rn1 := Random( 94868 );
21 rn2 := Random( 94868 );
22 rn3 := Random( 62575 );
23 Result:= 1000000000 + //既然是10位数这个是基数部分
24 rn1 * rn2 + rn3; //这一行随机数的范围囊括了: 0 到 8999999999 (94868*94868+62575=8999999999)
25 until Len10NumberList.IndexOf( IntToStr(Result) )=-1;//用INDEXOF的效率极高,
26 //用FOR循环是效率极低的,
27 //尤其是STRINGLIST中的条目很多的时候!!!
28 //这句为精华之处!!!
29 Len10NumberList.Add( IntToStr(Result) );//加到列表中以便下次查询,避免重复.
30 end;
31
32 procedure TForm1.FormDestroy(Sender: TObject);
33 begin
34 Len10NumberList.Free;//也可以做保存后FREE之
35 end;
36
37 procedure TForm1.Button1Click(Sender: TObject);
38 begin
39 ShowMessage( IntToStr( GenerateLen10Number ) );
40 end;
41
2 Form1: TForm1;
3 Len10NumberList:TStringList;
4
5 implementation
6
7 {$R *.dfm}
8
9 procedure TForm1.FormCreate(Sender: TObject);
10 begin
11 Len10NumberList:=TStringList.Create;
12 Randomize;
13 end;
14
15 function GenerateLen10Number:Int64;
16 var
17 rn1, rn2, rn3 : Int64;
18 begin
19 repeat
20 rn1 := Random( 94868 );
21 rn2 := Random( 94868 );
22 rn3 := Random( 62575 );
23 Result:= 1000000000 + //既然是10位数这个是基数部分
24 rn1 * rn2 + rn3; //这一行随机数的范围囊括了: 0 到 8999999999 (94868*94868+62575=8999999999)
25 until Len10NumberList.IndexOf( IntToStr(Result) )=-1;//用INDEXOF的效率极高,
26 //用FOR循环是效率极低的,
27 //尤其是STRINGLIST中的条目很多的时候!!!
28 //这句为精华之处!!!
29 Len10NumberList.Add( IntToStr(Result) );//加到列表中以便下次查询,避免重复.
30 end;
31
32 procedure TForm1.FormDestroy(Sender: TObject);
33 begin
34 Len10NumberList.Free;//也可以做保存后FREE之
35 end;
36
37 procedure TForm1.Button1Click(Sender: TObject);
38 begin
39 ShowMessage( IntToStr( GenerateLen10Number ) );
40 end;
41
关于indexof:
Call IndexOf to obtain the position of the first occurrence of the string S, or of a string that differs from S only by case. IndexOf returns the 0-based index of the string. Thus, if S matches the first string in the list, IndexOf returns 0, if S is the second string, IndexOf returns 1, and so on. If the string is not in the string list, IndexOf returns -1.
Note: If the string appears in the list more than once, IndexOf returns the position of the first occurrence.
没事,别怕,这是签名→→┃ 青山幽谷笛声扬,白鹤振羽任翱翔。往事前尘随风逝,携手云峰隐仙乡。 ┃