rand() 和 srand() 给出了奇怪的相似结果。来自 rand() 的 return 非常相似

rand() with srand() is giving strangely similar results. The return from rand() is very similar

这是一个看似常见的问题,所以我希望我不会听起来多余。但是从 rand() 返回的范围应该在 0 和 RAND_MAX 之间,然而,当我做一个非常简单的 rand 语句时,我总是在一个非常小的范围内得到 returns。

这个范围大约是 1,4XX,XXX,XXX。我想这可能是一个时钟问题,所以我等了三十分钟,我仍然得到相同范围内的数字。

这是二十分钟前的一些示例输出:

Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439810968
80
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439827775
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439827775
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439844582
78
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439878196
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1439895003
78

这是刚才的示例输出:

Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456483512
78
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456500319
80
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456500319
80
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456517126
29
Matthews-Macbook-Pro:Data_Structures matthewwright$ ./main
1456533933
78

我知道 rand() 并不完美,但这看起来太相似而不正确。如果范围是 0 - RAND_MAX,返回的每个数字都在同一范围内似乎很奇怪。

这是我正在测试的代码:

#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

int main(int argc, char const *argv[])
{
    /* declarations */
    srand(time(NULL));

    std::cout << std::rand() << std::endl;
    std::cout << std::rand()%100 << std::endl;
    return 0;
}

我认为我不需要所有这些 #include 语句,但我看到其他人在使用它们,所以我将它们包括在内以防它会影响我的输出,但它没有。

编辑

@Mgetz 和@Curious 提供的链接非常有用。巩固,

信息页面:http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

超级有帮助的讲座(真的,请观看):https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

我把听课的内容总结在自己的笔记上,免得忘记了下次再研究。我没有在这里写代码,大部分代码都在上面链接的 "Info page" 中。大多数评论都包含讲座中的信息,尽管不是逐字逐句地来自讲座。再一次,我真的推荐看那个。信息量大。

#include <iostream>
#include <random>

int main(int argc, char const *argv[])
{
    /* https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful */

    /* Randomness Verson 1 : Deterministic */
    std::mt19937 mt(1234);
    std::uniform_int_distribution<int> dist(0,127);
    /* Default is int, but we could specify others.
     * The range is [inclusive, inclusive]
     * 
     * Above is Mersenne Twister RNG. It is deterministic, meaning we can get the same result
     * if we use "std::mt19937 mt(1234)"; or something like that. This could be useful for some
     * people (He mentions games, some experiments, et cetera). It is stupid fast.
     * 
     * However, it isn't cryptographically secure, but it pretty random as random goes. If you
     * track the output though, you could guess the next numbers, so don't use it for anything
     * secure.
     */

    /* Randomness Verson 2 */
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<int> dis(0, 127); // Inclusive
    /* This is not reproducible. This is not deterministic.
     * "Possibly Crypto-secure." Seems like using Random Device makes this near perfect random,
     * assuming some conditions. I'm not a man who's written security software, and if you are 
     * writing security software, I assume you're not looking at Whosebug to figure out how
     * to do random numbers. The way he talked about it in the lecture made this seem much more 
     * secure, but I'm not sure what I'm talking about when it comes to these things
     */

    for (int i = 0; i < 3; ++i)
    {
        /* Below would output the pure Mersenne Twister output, deterministic. This seems to
         * be pretty random, but it isn't totally random. */
        std::cout << dist(mt) << " ";

        /* And below would output the random device output. This should be slower, but
         * more truly random. */

        //Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
        std::cout << dis(gen) << " ";

        std::cout<< std::endl;
    }
}

使用取模运算符会给结果带来一定程度的偏差 "random number"。此外,rand() 函数的工作是实现定义的,不遵循跨平台的标准算法。

考虑使用更现代的 C++11 随机数生成功能,这些功能使用标准的广泛接受的随机数生成算法,跨平台工作相同(当然给定相同的种子)。

请参阅 cppreference page for std::uniform_int_distribution

中的以下示例
#include <random>
#include <iostream>

int main()
{
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<> dis(1, 6);

    for (int n=0; n<10; ++n)
        //Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
        std::cout << dis(gen) << ' ';
    std::cout << '\n';
}

这是 link Stephan Levavej 的精彩演讲,更深入地探讨了这个问题 https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful