Excel VBA Rnd不实际随机 [英] Excel VBA Rnd Not Actually Random

查看:378
本文介绍了Excel VBA Rnd不实际随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个宏,当我点击一个按钮,它随机在msgbox中给我一个数字:

  dim number as double 
number = Int(8 * Rnd + 1) - 1
MsgBox number


$事实是,数字实际上并不是随机的。例如:如果我启动宏,请点击两次按钮,让我说我得到的数字5和2.现在如果我关闭宏并再次打开它,并点击按钮两次,我得到相同的两个数字5和2。



现在我知道在VB.net中有一种方法可以让它每次都能吐出随机数,而不会重复序列,但是自从我触摸vb.net所以我不太记得,我也不知道如何使用它在excel vba。

解决方案

您需要初始化随机数函数

  Sub test()
Dim number As Double
随机化
number = Int(8 * Rnd + 1) - 1
MsgBox号
End Sub


I currently have a macro that when i click a button, it "randomly" gives me a number in a msgbox:

dim number as double
number= Int(8 * Rnd + 1) - 1
MsgBox number

The thing is, the numbers aren't actually randomized. For example: If i start the macro, click the button twice, lets say i get the numbers 5 and 2. Now if i close the macro and open it again and click the button twice, i get the same two numbers 5 and 2.

Now i know that in VB.net there was a way to actually get it to spit out random numbers each time without repeating the "sequence" but it's been years since i touched vb.net so i don't quite remember, also i would not know how to use it in excel vba.

解决方案

You need to initialize the random number function

Sub test()
Dim number As Double
Randomize
number = Int(8 * Rnd + 1) - 1
MsgBox number
End Sub

这篇关于Excel VBA Rnd不实际随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆