C++ 生成 0 到 10 之间的随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5743678/
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-08-28 18:47:47  来源:igfitidea点击:

Generate Random number between 0 and 10

c++mathrandom

提问by karthik

How can i generate Random numbers between 0 and 10? Can I have a sample for this random number generation?

如何生成0到10之间的随机数?我可以为这个随机数生成提供一个样本吗?

回答by relaxxx

1) You shouldn't use rand(), it has bad distribution, short period etc...

1)你不应该使用rand()它,它有分布不好,周期短等......

2) You shouldn't use %xwhen MaxValue % x != 0because you mess your uniform distribution (suppose you don't use rand()), e.g. 32767 % 10 = 7so number 0-7 are more likely to get

2)你不应该使用%xwhenMaxValue % x != 0因为你弄乱了你的均匀分布(假设你不使用 rand()),例如32767 % 10 = 7数字 0-7 更有可能得到

Watch this for more info: Going native 2013 - Stephan T. Lavavej - rand() Considered Harmful

观看此了解更多信息:Going native 2013 - Stephan T. Lavavej - rand() 被认为有害

You should use something like:

你应该使用类似的东西:

#include <random>

std::random_device rdev;
std::mt19937 rgen(rdev());
std::uniform_int_distribution<int> idist(0,10); //(inclusive, inclusive) 

I in my codes use something like this:

我在我的代码中使用这样的东西:

template <typename T>
T Math::randomFrom(const T min, const T max)
{
    static std::random_device rdev;
    static std::default_random_engine re(rdev());
    typedef typename std::conditional<
        std::is_floating_point<T>::value,
        std::uniform_real_distribution<T>,
        std::uniform_int_distribution<T>>::type dist_type;
    dist_type uni(min, max);
    return static_cast<T>(uni(re));
}

NOTE: the implementation is not thread safe and constructs a distribution for every call. That's inefficient. But you can modify it for your needs.

注意:该实现不是线程安全的,并且为每个调用构造一个分布。那是低效的。但是您可以根据需要对其进行修改。

回答by karthik

  /* rand example: guess the number */
  #include <stdio.h>
  #include <stdlib.h>
  #include <time.h>

  int main ()
  {
        int iSecret, iGuess;

      /* initialize random seed: */
        srand ( time(NULL) );

      /* generate secret number: */
       iSecret = rand() % 10 + 1;

        do {
           printf ("Guess the number (1 to 10): ");
          scanf ("%d",&iGuess);
          if (iSecret<iGuess) puts ("The secret number is lower");
          else if (iSecret>iGuess) puts ("The secret number is higher");
        } while (iSecret!=iGuess);

      puts ("Congratulations!");
     return 0;
    }

iSecretvariable will provide the random numbers between 1 and 10

iSecret变量将提供 1 到 10 之间的随机数

回答by Fortega

random_integer = rand()%10;should do the trick I guess.

random_integer = rand()%10;我猜应该可以解决问题。

random_integer = rand()%11;for all numbers between 0 and 10, 10 included...

random_integer = rand()%11;对于 0 到 10 之间的所有数字,包括 10...