⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 模拟正态分布随机变量 将结果写入文件 算法是从一本数学书上改编而来
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "模拟正态分布随机变量"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   2  '屏幕中心
   Begin VB.CommandButton Command1 
      Caption         =   "Generate"
      Height          =   495
      Left            =   1800
      TabIndex        =   0
      Top             =   1320
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Base 1
Const Size = 100
Const E = 0         '期望
Const S = 1         '标准差

Private Sub Command1_Click()
    '打开文件
    Dim strFileName As String
    strFileName = App.Path & "\NormRnd.txt"
    'Dim FilehWnd As Integer
    'FilehWnd = FreeFile
    Open strFileName For Output As #1 'FilehWnd
    
    Dim Normals(Size) As Double
    '产生随机数
    For i = 1 To Size
        Normals(i) = NormalRndNums(E, S)
    Next i
    
    '写入文件
    For i = 1 To Size
        Print #1, Normals(i)
    Next i
    
    '关闭文件
    Close #1 'FilehWnd
End Sub

Private Function StandardNormalRndNums() As Double
    '标准正态分布
    Dim S As Double, t As Double, p As Double, q As Double
    Do While True
        S = 2 * Rnd - 1
        t = 2 * Rnd - 1
        p = S * S
        q = S * S + t * t
        If p < 1 Then Exit Do
    Loop
    q = Sqr((-2 * Log(p)) / p)
    StandardNormalRndNums = S * q
End Function

Private Function NormalRndNums(a As Double, b As Double) As Double
    '转化为一般正态分布
    Dim x As Double
    x = StandardNormalRndNums()
    NormalRndNums = a + b * x
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -