zoukankan      html  css  js  c++  java
  • 随机生成一个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 

    关于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.
  • 相关阅读:
    2016-09-13面试记录
    javascript中的深度拷贝的实现过程及深拷贝的几种方法。
    javascript中的for in循环
    常见的兼容问题及其解决方法。
    一次清空所有数据方法
    数组排序
    css对齐 挖坑~
    css reset样式重置
    CSS 表单
    CSS 表格
  • 原文地址:https://www.cnblogs.com/dabiao/p/1639370.html
Copyright © 2011-2022 走看看