Function GetRandomString: String;
const
Max_Len = 10;
var
i: Byte;
s: string;
begin
Randomize;
s :='0123456789';
Result := '';
for i := 0 to Max_Len-1 do
Result := Result + inttostr(Random(Length(s)));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s1: TStringList;
s:string;
a:integer;
b:integer;
begin
edit1.Text:=timetostr(time);
b:=strtoint(edit3.text);
s1:= TStringList.Create;
for a:=1 to b do
begin
s:=GetRandomString;
s1.Add(s);
end;
s1.SaveToFile('d:\ok.txt');
s1.free;
edit2.Text:=timetostr(time);
beep;
showmessage('完成');
end;
end.一次产生100万条10位的数字,偶尔会出现重复。想不通,random的 关系么? 请大家讨论一下。
const
Max_Len = 10;
var
i: Byte;
s: string;
begin
Randomize;
s :='0123456789';
Result := '';
for i := 0 to Max_Len-1 do
Result := Result + inttostr(Random(Length(s)));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
s1: TStringList;
s:string;
a:integer;
b:integer;
begin
edit1.Text:=timetostr(time);
b:=strtoint(edit3.text);
s1:= TStringList.Create;
for a:=1 to b do
begin
s:=GetRandomString;
s1.Add(s);
end;
s1.SaveToFile('d:\ok.txt');
s1.free;
edit2.Text:=timetostr(time);
beep;
showmessage('完成');
end;
end.一次产生100万条10位的数字,偶尔会出现重复。想不通,random的 关系么? 请大家讨论一下。
解决方案 »
- 自定义函数里 CreateForm 的怪事
- 各们高手,怎么制作漂亮的软件界面呀。
- 大家帮忙看看这个SQL语句错在哪里?
- 莫名其妙的问题
- 我使用DataSource,我需要对数据源编辑修改,他老提示不能插入,或修改模式!
- 特急!fastreport2.46无法显示TfrReport中的"对象查看器",如何恢复?
- 如何获取IE使用的当前Internet连接的名字?
- 我原来用的是InterBase4.2,现在想换成InterBase7.0,可以用吗,会不会有问题?
- 如何使用DELPHI调用CAD图纸
- 我想知道用DELPHI做开发一般有多少钱一个月(什么地方)?
- 请教关于table.locata的用法
- Delphi如何结束程序?为什么我的程序窗体结束了,但是程序未能结束?
还要兼顾速度啊。
要求不重复啊.不然何必要那样呢.
下次要用的时候,从第trunc(100000000*rand())个数字开始读取这个随机数表(超过1亿就从头读取)就可以快速生成1亿个不重复的随机数了.
下次再聊.
前一段时间自己也做过有关随机数的问题~
用了三个Random,生成三个随机数~然后还用了一些TIME
用来不停的生成三位随机数~
发现每次起动程序第一次生成的数都是同一个数:114
当然后面的生成还不和每次的不一样~
就是这个固定的:114不知道是怎么一回事~
如果有兴趣研究的朋友可以到这个网址里面去下载看看: http://dianhui.ys168.com
程序源代码和.EXE文件都放在这里面~可以去下载~
一般情况下,编程语言(C/C++/VB/Delphi)里的随机数函数用的都是一个固定的数表,通过初始化语句改变不同的起点,如果不用初始化语句就会得到固定的数字序列,据说是为了便于调试.如果用要用到真正的随机数,要从现实中去采集.但是这样采集出来的随机数也未必适合楼主的程序,1百万个数字中可能会有重复.
var
i, M, R: Integer;
S: String;
B: TBits;
F: TextFile;
begin
if not (aLen in [2..10]) then
Exit;
M := Round(IntPower(10, aLen));
if aCount * 2 > M then
Exit; S := '%.' + IntToStr(aLen) + 'd';
Randomize;
AssignFile(F, FileName);
Rewrite(F);
try
B := TBits.Create;
try
B.Size := M;
for i := 0 to aCount - 1 do
repeat
R := Random(M);
if not B[R] then
begin
B[R] := True;
Writeln(F, Format(S, [R]));
Break;
end;
until False;
finally
B.Free;
end;
finally
CloseFile(F);
end;
end;procedure TForm1.Button1Click(Sender: TObject);
const // *%+-*%+-
CountGet: Integer = 100000000; //总数
NumberLen: Byte = 10; //10位数字
begin
Button2.Caption := 'Writing';
SaveRandomNumToFile(NumberLen, CountGet, GetCurrentDir +'\2.Txt');
Button2.Caption := 'OK';
end;
end.