zoukankan      html  css  js  c++  java
  • 获取随机字符串的方法 GetRandomString

    方法1:推荐方便。

    System.Hash 单元

    Memo1.Lines.Add(THash.GetRandomString(50));

    方法二(自己写的):

    复制代码
    function TStrApi.SuiJiString(const AWeiShu: Integer): string;
    const
      SourceStr: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var
      MyRep: string;
      I: Integer;
    begin
      Randomize;
      for I := 1 to AWeiShu do
      begin
        //这里只所以必须加1,是因为SourceStr是从1开始的,而Random是从0开始的,SourceStr[0]就会报错,SourceStr[63]也会报错
        MyRep := MyRep + SourceStr[Random(61)+1];
      end;
      Exit(MyRep);
    end;
    复制代码

    PK结果,效率差不多。:

    复制代码
    procedure TForm6.Button1Click(Sender: TObject);
    var
      I: Integer;
      startTime: Cardinal;
      strResult: string;
    begin
      startTime := GetTickCount;
      for I := 1 to 10000 do
      begin
        strResult := THash.GetRandomString(50);
      end;
      Label1.Caption := '耗时: ' + (GetTickCount - startTime).ToString;
    end;
    
    procedure TForm6.Button2Click(Sender: TObject);
    const
      SourceStr: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var
      strResult: string;
      I,K: Integer;
      startTime: Cardinal;
    begin
      startTime := GetTickCount;
      for I := 1 to 10000 do
      begin
        Randomize;
        strResult := '';
        for K := 1 to 50 do
        begin
          //这里只所以必须加1,是因为SourceStr是从1开始的,而Random是从0开始的,SourceStr[0]就会报错,SourceStr[63]也会报错
          strResult := strResult + SourceStr[Random(61)+1];
        end;
      end;
      Label1.Caption := '耗时: ' + (GetTickCount - startTime).ToString;
    end;
    复制代码

     http://www.cnblogs.com/del88/p/6911709.html

  • 相关阅读:
    如何优雅地关闭资源
    JMeter使用教程2——MySQL压测
    JMeter使用教程
    Redis系列(五):消息队列
    Redis系列(四):地理信息
    Redis系列(三):Bitmaps和HyperLogLog
    Redis系列(二):常用操作
    Redis系列(一):安装
    linux命令学习_实验楼(总结)
    【转】Linux中常用的tar解压打包命令语法介绍
  • 原文地址:https://www.cnblogs.com/findumars/p/9398407.html
Copyright © 2011-2022 走看看