delphi 随机函数单元uRandomUtils


unit uRandomUtils; //晓不得2013 QQ:26562729

interface

uses system.math, system.SysUtils;

function GetRandomCharStr(ADig: integer): string;
function GetRandomNumStr(ADig: integer): string; overload;
function GetRandomNumStr(AStart, AEnd: integer): string; overload;
function GetRandomHanziStr(ADig: integer): string;

implementation

type
  TGetRandomChar = reference to function: Char;

const
  HanZi = '平丽强勇军梅建化国中天莉尚丰田开杰锦思小晓伦春满淋叶风亚唯赵';

function GetRandomString(ADig: integer; AGetRadomChar: TGetRandomChar): string; overload;
var
  i: integer;
begin
  Result := '';
  if ADig < 1 then exit;
  for i := 1 to ADig do
  begin
    Randomize;
    Result := Result + AGetRadomChar;
  end;
end;

function GetRandomCharStr(ADig: integer): string;
var
  AChar: TGetRandomChar;
begin
  AChar := function: Char
    begin
      Result := Char(RandomRange(65 + 32, 91 + 32));
    end;
  Result := GetRandomString(ADig, AChar);
end;

function GetRandomNumStr(ADig: integer): string;
var
  AChar: TGetRandomChar;
begin
  AChar := function: Char
    begin
      Result := inttostr(RandomRange(0, 10))[1];
    end;
  Result := GetRandomString(ADig, AChar);
end;

function GetRandomNumStr(AStart, AEnd: integer): string; overload;
begin
  Result := IntToStr(RandomRange(AStart, AEnd + 1));
end;

function GetRandomHanziStr(ADig: integer): string;
var
  AChar: TGetRandomChar;
  nHanzi: integer;
begin
  nHanzi := length(HanZi) + 1;
  if nHanzi <= 1 then exit('');
  AChar := function: Char
    begin
      Result := HanZi[RandomRange(1, nHanzi)];
    end;
  Result := GetRandomString(ADig, AChar);
end;

end.


联系电话:
020-00000000
联系电话:
020-00000000
联系电话:
020-12345678