简体   繁体   English

为什么rand()的范围小于int的范围?

[英]Why is the range of rand() smaller than that of int?

I am trying to fill a vector with 5000 random numbers. 我正在尝试用5000个随机数填充向量。 Each number is an integer. 每个数字都是整数。 But I am wondering why rand() only gives positive integers, and also why it never gives the maximum values that an integer can hold. 但是我想知道为什么rand()只给出正整数,为什么它从不给出整数可以容纳的最大值。 Is there a reason for that? 有什么理由吗?

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{

unsigned seed = time(0);
srand(seed);

vector<int> vect(5000);

for (int i = 0; i < vect.size(); i++)
{
    vect[i] = rand();
}
return 0;
}

Here's what rand() does: 这是rand()作用:

Returns a pseudo-random integral value between ​0​ and RAND_MAX ( 0 and RAND_MAX included). 返回介于​0RAND_MAX之间的伪随机整数值(包括0RAND_MAX )。

RAND_MAX is implementation defined but will be "at least 32767". RAND_MAX是实现定义的,但将为“至少32767”。

There is no "why" other than "this is how it is specified". 除了“这是如何指定”之外没有其他“为什么”。

If you want a range that includes some negative numbers, you can apply some maths (for example, a subtraction). 如果要包含一个负数的范围,则可以应用一些数学运算(例如,减法)。

Do note: 注意事项:

rand() is not recommended for serious random-number generation needs. 不建议将rand()用于严重的随机数生成需求。 It is recommended to use C++11's random number generation facilities to replace rand() . 建议使用C ++ 11的随机数生成工具来替换rand() (since C++11) (自C ++ 11起)

Said facilities give you better control and better distribution ( example ). 所述设施为您提供了更好的控制和更好的分配( 示例 )。

The reason why you're not getting the full possible range is because rand() is ancient. 为什么你没有得到完整的可能范围的原因是rand()是古。

Back in the days when rand() was developed, the common way to implement a random-number generator was a so-called Linear Congruential Generator (lcg). 早在rand()开发时,实现随机数生成器的常用方法就是所谓的线性同余生成器(lcg)。 This doesn't give very random numbers; 这不会给出非常随机的数字。 in fact they're quite predictable if you've seen the first few. 实际上,如果您看过前几个,它们是可以预见的。 But computers back then were slow and worked on small numbers. 但是,当时的计算机运行缓慢,只能处理少量数据。

That's why rand() only promises 15 bits - the constant RAND_MAX defines the maximum value you can get from RAND , and it can be as low as 32767 . 这就是rand()仅承诺15位的原因-常量RAND_MAX定义了您可以从RAND获得的最大值,并且该值可以低至32767 32 bit computers back then were not so common, so using 16 bits numbers made sense. 那时的32位计算机并不常见,因此使用16位数字是合理的。 And with common LCG implementations, the lowest bit was worthless (toggled between 0 and 1) so you wanted to discard that anyway. 对于常见的LCG实现,最低位是毫无价值的(在0和1之间切换),因此无论如何您都希望将其丢弃。

Several decades of improvement have resulted in much better generators. 几十年来的改进已使发电机更好了。 In particular, the mt19937 generator is very good and still very efficient. 特别是, mt19937生成器非常好,但仍然非常高效。 Since 2011, C++ has <random> and it includes std::mt19937 . 自2011年以来,C ++具有<random> ,其中包括std::mt19937 Also, for convenience you can now pick your own distribution, with std::uniform_int_distribution<>(min,max) 另外,为方便起见,您现在可以使用std::uniform_int_distribution<>(min,max)选择自己的分布

As mentioned rand() is deprecated and should never be used in practical scenarios. 如前所述,不建议使用rand() ,并且切勿在实际情况下使用它。 But to answer your question as to why only positive integers, it's because producing a negative random integer is redundant so you can just use basic math to make a negative number out of the positive random number that was produced. 但是要回答关于为什么只使用正整数的问题,这是因为生成负随机整数是多余的,因此您可以使用基本数学从生成的正随机数中生成负数。

To give you an example: If I wanted to produce a random number between -5 and 27 completely arbitrary, I could just produce a random number between 0 and 32(inclusive) then subtract 5. 举个例子:如果我想产生一个介于-5和27之间的完全随机的随机数,我可以产生一个介于0和32(含)之间的随机数,然后减去5。

#include <stdio.h>
#include <stdlib.h>
int main(){

    int random = (rand() % 33) - 5;

    printf("%d",random);

    return 0;
}

Because this isn't a practical scenario I did not seed the random either. 因为这不是实际情况,所以我也没有植入随机数。 So you should not use this in your code but it holds true for pretty much any RNG function where you just subtract the negative range from your positive. 因此,您不应该在代码中使用此功能,但是它几乎适用于任何RNG函数,您只需从正值中减去负值范围即可。 You should however use the C++11 random number generator. 但是,您应该使用C ++ 11随机数生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM