vba 生成 1 到 20 之间的随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22627708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:01:10  来源:igfitidea点击:

generating a random number between 1 and 20

vba

提问by luke123

The code I previously used was

我之前使用的代码是

Randomvariable = 1 + Int((20 - 1 + 1) * Rnd())

I'm a bit concerned it wasn't right because the format I'm seeing online in guides is

我有点担心这是不对的,因为我在网上看到的指南格式是

random_number = Int(20 * Rnd) + 1

Was my way equivalent to this?

我的方式是否与此相同?

回答by

Have a read about the Rnd()function

阅读有关Rnd()函数的信息

The general formula is

一般公式是

Int ((upperbound - lowerbound + 1) * Rnd + lowerbound)

Therefore if you want to generate random numbers between 1 and 20 use

因此,如果您想生成 1 到 20 之间的随机数,请使用

Int((20 - 1 + 1 ) * Rnd + 1)