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.
  • 相关阅读:
    Windows 10 IoT Core 17101 for Insider 版本更新
    Windows 10 IoT Core 17093 for Insider 版本更新
    Windows 10 IoT Serials 10 – 如何使用OCR引擎进行文字识别
    2017年全国大学生物联网设计竞赛(TI杯)华东分赛区决赛总结
    Windows 10 IoT Core 17083 for Insider 版本更新
    My Feedback for Windows 10 IoT Core on Feedback Hub App (4/1/2017-1/23/2018)
    为 Azure IoT Edge 设备部署 Azure Stream Analytics 服务
    Azure IoT Edge on Raspberry Pi 3 with Raspbian
    Azure IoT Edge on Windows 10 IoT Core
    Mixed Reality-宁波市VR/AR技术应用高研班总结
  • 原文地址:https://www.cnblogs.com/dabiao/p/1639370.html
Copyright © 2011-2022 走看看