Function GenerateSeed() As Long ' 获取当前时间的小数部分,乘以 1000 以增加精度 Dim currentTime As Double currentTime = Timer * 1000 ' 将时间值四舍五入为整数,作为种子值 GenerateSeed = Round(currentTime, 0) End Function Function MyRandom(Optional ByVal seed As Long = -1) As Variant Dim randomSeed As Long Dim randomNumber As Double ' 检查传入参数是否为Long类型 If Not IsNumeric(seed) Then MsgBox "随机数种子不是正整数,请修改或选择‘是否使用已有随机数’时选择否", vbExclamation, "错误" Exit Function End If If seed <> -1 Then randomSeed = seed Else randomSeed = GenerateSeed() End If ' 使用指定的种子值调用 Rnd 函数生成随机数 randomNumber = Rnd(-randomSeed) MyRandom = Array(randomSeed, randomNumber) End Function Sub test() Dim result As Variant Dim seed As Long ' 指定种子 seed = 56551340 result = MyRandom(seed) Debug.Print "种子:" & result(0) & ",随机数:" & result(1) ' 或者不指定种子 'result = MyRandom() 'Debug.Print "种子:" & result(0) & ",随机数:" & result(1) Debug.Print Rnd Debug.Print Rnd Debug.Print Rnd Debug.Print Rnd End Sub
下载方式
在审计军火库:
https://www.auditdog.cn/posts/readme/
中,打开
tools for auditor
->
05-抽凭模板
。