上次給出了C語言編寫的正態(tài)分布隨機(jī)數(shù)程序,,這次是VB的,,轉(zhuǎn)載請注明出處
Private Sub Command1_Click()
Dim i, n As Integer
Dim mu, sigma As Double
mu = Text1.Text
sigma = Text2.Text
n = Text3.Text
Open "data.txt" For Output As #1
For i = 0 To n
If i = n Then Exit For
Text4.Text = Text4.Text & Str(sta(mu, sigma)) & Chr(9)
Write #1, sta(mu, sigma)
Next
Close #1
End Sub
Function sta(mu, sigma As Double) As Double
Dim r1, r2 As Double
Dim pi As Double
pi = 3.1415926
r1 = Rnd
r2 = Rnd
sta = Int(Sqr(-2 * Log(r1)) * Cos(2 * pi * r2) * sigma + mu)
End Function