51代码网ORACLEMYSQLSQL SERVER其它数据库java/jspasp/asp.netC/C++/VC++APP应用其它语言服务器应用
您现在的位置: 51代码网 >> 其它 >> 文章正文

delphi求10位唯一值随机数源代码

更新时间:2013-2-17:  来源:51代码网

delphi求10位唯一值随机数源代码
delphi求10位唯一值随机数(含数字+字母)
字母最好都用大写
随机生成10位 唯一的值
就像生成guid一样 guid 32位太长了。   所以要弄个10位产度的。
有人做过吗? 还请指教 谢谢。。

下边的例子, 改造一下即可(可用字符列表中加入你可选的数字/字符)
01.unit Unit11; 
02. 
03.interface 
04. 
05.uses 
06.  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
07.  Dialogs, StdCtrls; 
08. 
09.type 
10.  TForm11 = class(TForm) 
11.    btn1: TButton; 
12.    procedure btn1Click(Sender: TObject); 
13.  private 
14.    //生成的随机数函数  
15.    function CreateRandom(const RangeStart, RangeEnd, 
16.      ResultCount: integer): Tarray<Integer>; 
17.    { Private declarations } 
18.  public 
19.    { Public declarations } 
20.  end; 
21. 
22.var 
23.  Form11: TForm11; 
24. 
25.implementation 
26. 
27.{$R *.dfm} 
28.//CreateRandom(随机数取值范围起始值, 终止值; 返回数组长度)  
29.function TForm11.CreateRandom(const RangeStart, RangeEnd, ResultCount : integer) : Tarray<Integer>; 
30.var 
31.  tmpLst : TStringList; //可以使用泛型列表或数组代替  
32.  I, n: Integer; 
33.begin 
34.  if RangeStart >= RangeEnd then 
35.    raise Exception.Create('错误的随机数范围!'); 
36. 
37.  if ResultCount < 1 then 
38.    raise Exception.Create('随机数返回数组长度必须大于0!'); 
39. 
40.  tmpLst := TStringList.Create; 
41.  tmpLst.Capacity := RangeEnd - RangeStart; 
42.  try 
43.    for n := RangeStart to RangeEnd do 
44.    begin 
45.      tmpLst.Add(IntToStr(n)); 
46.    end; 
47. 
48.    Randomize; 
49. 
50.    SetLength(Result, ResultCount); 
51.    for I := 0 to ResultCount do 
52.    begin 
53.      n := Random(tmpLst.Count - 1); 
54.      Result[i] := StrToInt(tmpLst.Strings[n]); 
55.      tmpLst.Delete(n); 
56.    end; 
57.  finally 
58.    tmpLst.free; 
59.  end; 
60.end; 
61. 
62.procedure TForm11.btn1Click(Sender: TObject); 
63.var 
64.  MyArr : Tarray<Integer>; 
65.  I : Integer; 
66.  s : string; 
67.begin 
68.  s := ''; 
69.  MyArr := CreateRandom(0, 100, 10); 
70.  for I := Low(MyArr) to High(MyArr) do 
71.    s := s + inttostr(Myarr[i]) + ' '; 
72. 
73.  ShowMessage(s); 
74. 
75.  s := ''; 
76.  MyArr := CreateRandom(100, 1000, 15); 
77.  for I := Low(MyArr) to High(MyArr) do 
78.    s := s + inttostr(Myarr[i]) + ' '; 
79. 
80.  ShowMessage(s); 
81.end; 
82. 
83.end. 

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 赞助商链接
    推荐文章
  • 此栏目下没有推荐文章
  • {
    设为首页 | 加入收藏 | 友情链接 | 网站地图 | 联系站长 |