C++ 随机数 1-9
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4621191/
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
C++ random number 1-9
提问by Ilyssis
I need random numbers from 1 to 9 (without 0).
我需要从 1 到 9(没有 0)的随机数。
//numbers 0 to 9
int iRand = rand() % 10;
But I need 1 to 9.
但我需要 1 到 9。
Thanks.
谢谢。
回答by Oliver Charlesworth
Just this:
只是这个:
int iRand = (rand() % 9) + 1;
回答by GManNickG
Well, you know how to get a random integer in the range [0, x], right? That's:
好吧,您知道如何在 [0, x] 范围内获得一个随机整数,对吗?那是:
rand() % (x + 1)
In your case, you've set x to 9, giving you rand() % 10
. So how can you manipulate a range to get to 1-9? Well, since 0 is the minimum value coming out of this random number generator scheme, we know we'll need to add one to have a minimum of one:
在您的情况下,您已将 x 设置为 9,从而为您提供rand() % 10
. 那么你如何操纵一个范围来达到 1-9?好吧,因为 0 是这个随机数生成器方案的最小值,我们知道我们需要加一才能得到最小值:
rand() % (x + 1) + 1
Now you get the range [1, x + 1]. If that's suppose to be [1, 9], then x must be 8, giving:
现在你得到了范围 [1, x + 1]。如果假设为 [1, 9],则 x 必须为 8,给出:
rand() % 9 + 1
That's how you should think about these things.
这就是你应该如何考虑这些事情。
回答by Lightness Races in Orbit
To initialize the random number generator call srand(time(0));
Then, to set the integer x
to a value between low
(inclusive) and high
(exclusive):
初始化随机数生成器调用srand(time(0));
然后,将整数设置为(包含)和(不包含)x
之间的值:low
high
int x = int(floor(rand() / (RAND_MAX + 1.0) * (high-low) + low));
The floor()
is not necessary if high and low are both non-negative.
floor()
如果 high 和 low 都是非负数,则不需要。
Using modulus (%
) for random numbers is not advisable, as you don't tend to get much variation in the low-order bits so you'll find a very weak distribution.
%
对随机数使用模数 ( ) 是不可取的,因为您不会在低阶位中获得太多变化,因此您会发现分布非常弱。
回答by Cameron
Try:
尝试:
int iRand = 1 + rand() % 9;
It works by taking a random number from 0 to 8, then adding one to it (though I wrote those operations in the opposite order in the code -- which you do first comes down to personal preference).
它的工作原理是取一个从 0 到 8 的随机数,然后给它加一个(尽管我在代码中以相反的顺序编写了这些操作——你首先要根据个人喜好来做这些操作)。
Note that %
has higher precedence than +
, so parentheses aren't necessary (but may improve readability).
请注意, 的%
优先级高于+
,因此不需要括号(但可能会提高可读性)。
回答by aronp
How about
怎么样
int iRand = (rand() % 9) + 1;
doh beaten by seconds
doh 被秒打败
回答by Schultz9999
Regardless that the answer is picked, modulo based ranging is biased. You can find that all over the internet. So if you really care about that then you have to do a bit more than that (assume arc4random() return a 4 byte integer):
不管选择了答案,基于模的测距是有偏差的。你可以在互联网上找到。因此,如果您真的关心这一点,那么您必须做更多的事情(假设 arc4random() 返回一个 4 字节整数):
#define NUMBER 9.0
#define RANDOM() (((int)(arc4random() / (float)0xffffffff * (float)NUMBER) + 1) % (NUMBER + 1))
I leave it to you to figure out the correct syntax since you sound like a quite capable developer.
我把它留给你来找出正确的语法,因为你听起来像是一个非常有能力的开发人员。