【问题标题】:How do i Pick a Random Number from an array in c++? [closed]如何从 C++ 中的数组中选择一个随机数? [关闭]
【发布时间】:2018-09-21 14:33:49
【问题描述】:
int array[5];
int Random;
for (int i = 0; i <5; i++)
{
    cin>>array[i];
}
for (int j = 0; j < 5; j++)
{
    Random = array[rand() % array[j]];
}
cout << Random << endl;

这让我不断返回 1,但我每次都想要不同的数字

【问题讨论】:

标签: c++ random


【解决方案1】:

兰德基本上已经过时了。
有很多抱怨它有多糟糕(因为要正确使用它,你必须记住做几件事)。即使是 Peris 在他的回答中也不能纠正不均匀的范围。

所以请尝试使用更强大的现代随机库。尽管它的文档很难阅读,但您不需要全部阅读。这是一个如何使用它的简单示例。

#include <random>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int>  array(5, 0);
    for (int i = 0; i < 5; ++i)
    {
        std::cin >> array[i];
    }

    std::random_device rd;
    std::mt19937       gen(rd());
    std::uniform_int_distribution<> dis(0, array.size() - 1);

    std::cout << array[dis(gen)];
}

注意事项:

rd:      Random device. Gives some initial randomness to help initialize things.
         Think of this as the `srand(time())` in the old random (but better).

mt19937: This is the algorithm used to generate the random number.
         It is initialized with some random value from rd. But it
         is a well know well understood random algorithm.

         Also be seperating this out into its own object.
         We don't have a central random number place. This means
         different applications can have their own unique random number stream.

         Note: If you want a random number stream. But the same stream
         every time (testing/debugging) then don't use the random
         device to initialize this object. Just use a nice normal
         integer and it will give a random stream of numbers but each
         time you run the application you get the same stream (which is useful
         for debugging when you don't actually want real randomness).


dis:     The main issue with the old rand() is that if just gave a number.
         It was up to the user of the number to build appropriate distributions
         and to be blunt most people either got it wrong or did not bother.
         In the random library there are several built in distributions but uniform
         is a nice easy one that is often useful (and I hope obvious).

【讨论】:

    【解决方案2】:

    rand() 不返回真随机,而是返回伪随机。 这完全取决于您提供给随机生成器的初始种子。如果初始种子相同,那么您从伪随机算法中得到的结果数是相同的。

    然后您应该在每次调用时更改rand() 的初始种子(在这种情况下,每次执行程序)。还有什么比time 更好的变化值?

    注意:

    您的代码中的array[rand() % array[j]]; 行极易受到数组索引越界导致分段错误的影响。

    这是解决方案。

    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    int main()
    {   
        // Initialize the srand seed.
        srand (time(NULL));
    
        int size = 5;
        int array[size];
        int Random;
        for (int i = 0; i <5; i++)
        {
            cin>>array[i];
        }
    
        int index = rand() % size;
        Random = array[index];
        cout << Random << endl;
    }
    

    更新

    正如许多其他人建议的那样,您可以转到std::uniform_int_distribution 以获得更好的结果。我的回答只会更新你的初始代码。

    【讨论】:

    • 附录:考虑使用&lt;random&gt; library 中的std::uniform_int_distribution 而不是srand/rand。如果不想重复,请查看std::shuffle's documentation page底部的示例
    • int size = 5; int array[size]; 不是有效的 C++。一些编译器将其作为扩展提供。使size 成为const;在这里没有必要对其进行修改,即使它是合法的。
    • 如果你打算使用rand(),请看在上帝的份上,确保范围是好的。丢弃任何大于RAND_MAX / size * size 的 rand() 值,否则您的概率分布不会均匀分布在所有成员中。
    猜你喜欢
    • 2013-10-08
    • 2013-01-12
    • 2017-03-11
    • 2021-12-31
    • 2020-03-06
    • 1970-01-01
    • 2020-10-04
    • 2020-07-29
    • 2010-12-10
    相关资源
    最近更新 更多