Excel VBA Rnd不实际随机 [英] Excel VBA Rnd Not Actually Random
问题描述
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屋!