gpt4 book ai didi

delphi - 使用delphi快速生成随机样本数据的方法

转载 作者:行者123 更新时间:2023-12-03 14:58:04 29 4
gpt4 key购买 nike

我有这样的结构

const
MaxSignalRecords=255;
type
TSignalRecord=record
signal1 : integer;
signal2 : integer;
signal3 : integer;
signal4 : integer;
signal5 : integer;
signal6 : integer;
bsignal1 : Boolean;
bsignal2 : Boolean;
bsignal3 : Boolean;
bsignal4 : Boolean;
bsignal5 : Boolean;
bsignal6 : Boolean;
end;

TListSignals = Array[0..MaxSignalRecords-1] of TSignalRecord;

以及生成随机样本数据的过程

Procedure FillRandomListSignals(var ListSignals:TListSignals);
var
i :Integer;
begin
for i := 0 to MaxSignalRecords - 1 do
with ListSignals[i] do
begin
signal1 :=Random(MaxInt);
signal2 :=Random(MaxInt);
signal3 :=Random(MaxInt);
signal4 :=Random(MaxInt);
signal5 :=Random(MaxInt);
signal6 :=Random(MaxInt);
bsignal1 :=Boolean(Random(2));
bsignal2 :=Boolean(Random(2));
bsignal3 :=Boolean(Random(2));
bsignal4 :=Boolean(Random(2));
bsignal5 :=Boolean(Random(2));
bsignal6 :=Boolean(Random(2));
end;
end;

如何提高 FillRandomListSignals 过程的性能?

编辑:该结构用于进行数千(可以是数百万)次计算

for i:=1 to 1000000 do
begin
CleartheList(MyList);
FillRandomListSignals(MyList);
DotheMath(MyList);
DotheChart(MyList);
end;

最佳答案

当您生成随机数据时,速度并不是您唯一关心的问题,您实际上希望该数据是随机的,您不希望您的实验受到重复数据或其他伪随机生成器问题的困扰。如果您更关心速度而不是随机性,您可以随时使用 function like this one ,那将是超快的! </joke> .

Here's a post by Barry Kelly on Stack Overflow描述内置随机数生成器可能出现的问题。这里就不引用了,自己去读一下吧,很好的东西。

为了得出结论,当我需要一个足够好的 PRNG 来生成大量随机数据时,我使用了 Mersenne Twister (wikipedia link) ,由 Delphi 的 PRNG 播种。

引用维基百科上关于 Mersene Twister 的内容:

For many applications the Mersenne twister is quickly becoming the pseudorandom number generator of choice. The Mersenne Twister is designed with Monte Carlo simulations and other statistical simulations in mind. Researchers primarily want high quality numbers but also benefit from its speed and portability.

为了打破每个帖子链接数量的所有记录,我使用了 this Delphi implementation .

我的最后一个想法:除非您非常擅长数学,否则请远离自制的 PRNG 实现。就像哈希函数一样,很容易出错,而且很难分析。

<小时/>

编辑

使用以下代码进行了一些计时。使用 Mersenne Twister 生成 10,000,000 条记录需要 1480 毫秒。对于相同的 10M 记录,使用 Delphi 内置随机数生成器的相同代码仅花费 250 毫秒。有件事告诉我,需要优化的不是随机生成器,而是代码中的其他内容。

procedure TForm1.Button1Click(Sender: TObject);
var InitArray:array[0..99] of LongInt;

i, N:Integer;
TSR: TSignalRecord;

CStart, CStop: Int64;

begin
Randomize;
for i:=0 to 99 do InitArray[i] := Random($effffff);
InitMTbyArray(InitArray, Length(InitArray));

CStart := GetTickCount;

for i:=1 to 10000000 do
begin
TSR.signal1 := IRanMT;
TSR.signal2 := IRanMT;
TSR.signal3 := IRanMT;
TSR.signal4 := IRanMT;
TSR.signal5 := IRanMT;
TSR.signal6 := IRanMT;

N := IRanMT;

TSR.bsignal1 := (N and 1) <> 0;
TSR.bsignal2 := (N and 2) <> 0;
TSR.bsignal3 := (N and 4) <> 0;
TSR.bsignal4 := (N and 8) <> 0;
TSR.bsignal5 := (N and 16) <> 0;
TSR.bsignal6 := (N and 32) <> 0;
end;

CStop := GetTickCount;

Caption := IntToStr(CStop - CStart);
end;

关于delphi - 使用delphi快速生成随机样本数据的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5509427/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com