RandG function
Generates random numbers with Gaussian distribution.
Unit
Math
Category
random number routines
Delphi syntax:
function RandG(Mean, StdDev: Extended): Extended;
C++ syntax:
extern PACKAGE Extended __fastcall RandG(Extended Mean, Extended StdDev);
Description
RandG produces random numbers with Gaussian distribution about the Mean. This is useful for simulating data with sampling errors and expected deviations from the Mean.
又学了一招
var
rv,rv1,rv2,rv3,rv4,i:integer;
begin
rv1:=0;
rv2:=0;
rv3:=0;
rv4:=0;
i:=0;
repeat
rv:=random(100);
rv:=random(rv);
rv:=random(rv);
if (rv>=0) and (rv<25) then
rv1:=rv1+1;
if (rv>=25) and (rv<50) then
rv2:=rv2+1;
if (rv>=50) and (rv<75) then
rv3:=rv3+1;
if (rv>=75) and (rv<=100) then
rv4:=rv4+1;
i:=i+1;
until i=strtoint(edit5.Text);
edit1.Text:=inttostr(rv1);
edit2.Text:=inttostr(rv2);
edit3.Text:=inttostr(rv3);
edit4.Text:=inttostr(rv4);
end;
我写了一个函数,但只统计了30000个记录
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.DrawAxis;
begin
imgAxis.Canvas.Pen.Color :=clWhite;
imgAxis.Canvas.Pen.Mode :=pmXor;
imgAxis.Canvas.MoveTo(imgAxis.Width div 2,imgAxis.Height);
imgAxis.Canvas.LineTo(imgAxis.Width div 2,0);
imgAxis.Canvas.MoveTo(0,imgAxis.Height -10);
imgAxis.Canvas.LineTo(imgAxis.Width,imgAxis.Height -10);
end;
procedure TfrmMain.ReDraw;
var
bmp :TBitmap;
i :Integer;
begin
//根据数组里面的数和totalCount重新画图
bmp :=TBitmap.Create;
try
bmp.Width :=imgAxis.Width;
bmp.Height:=imgAxis.Height;
bmp.Canvas.Brush.Color :=clBlack;
bmp.Canvas.FillRect(bmp.Canvas.ClipRect);
bmp.Canvas.Pen.Color :=clRed;
bmp.Canvas.Pen.Width :=1;
//根据Image高度和1->1000发生的频数设置画线的高度
//应该计算出图像所能反映的最小分辨率,应能体现到频数有1的变化
for i:=1 to 1000do
begin
sampleDatas :=(sampleDatas*(bmp.Height-10)) *200 div totalCount;//频数->频率->实际图像高度值
sampleDatas :=bmp.Height-10 -sampleDatas;//高度转换为实际坐标
bmp.Canvas.MoveTo(i,bmp.Height-10);
bmp.Canvas.LineTo(i,sampleDatas);
end;
//Log('height.txt');
imgAxis.Canvas.Draw(0,0,bmp);
finally
bmp.Destroy;
end;
end;
procedure TfrmMain.GaussIt(const ASampleData:Extended);
var
x :Integer;
begin
Inc(totalCount);
//ShowMessage(FloatToStr(ASampleData));
//将这个随机数映射到1..1000,正数:500->1000,负数500->1
//ASampleData>=2 ---1000
// =0 ---500
// <=-2 ---1
x :=Round(ASampleData *1000);
//确定范围
if x>=4000 then
x:=4000
else
if x<=-4000 then
x:=-4000;
x := (x +4001) shr 3;
Inc(sampleDatas[x]);//记录发生的频数,除以总次数就是频率了
//ShowMessage(IntToStr(x));
//ShowMessage(IntToStr(sampleDatas[x]));
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
do
ubleBuffered :=true;
DrawAxis;
end;
procedure TfrmMain.btnRandGClick(Sender: TObject);
var
i:Integer;
begin
totalCount :=0;
for i:=1 to 1000do
sampleDatas:=0;
for i:=1 to 100000do
//总计100000次
GaussIt(RandG(0,1));//标准正态分布
//Log('afterBtn.txt');
ReDraw;
DrawEdge;
DrawAxis;
end;
procedure TfrmMain.FormShow(Sender: TObject);
begin
Left :=0;
end;
procedure TfrmMain.Log(const logName:string);
var
i:Integer;
log:TStringList;
str:string;
begin
log :=TStringList.Create;
try
for i:=1 to 1000do
begin
str :=Format('%.4d : %d(频数)',[i,sampleDatas]);
log.Add(str);
end;
str :=Format('总数:%d',[totalCount]);
log.Add(str);
totalCount :=0;
for i:=1 to 1000do
inc(totalCount,sampleDatas);
str :=Format('实际总数:%d',[totalCount]);
log.Add(str);
log.SaveToFile(logName);
finally
log.Destroy;
end;
end;
procedure TfrmMain.DrawEdge;//画边
var
i:Integer;
points :array[1..1000] of TPoint;
begin
for i:=1 to 1000do
begin
points.x :=i;
points.y :=sampleDatas;
end;
imgAxis.Canvas.Polyline(points);
end;