C++ 用C++生成1到3之间的随机数

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

Generate random number between 1 and 3 in C++

c++random

提问by Petefic

Possible Duplicate:
Generating random integer from a range

可能的重复:
从一个范围生成随机整数

I just started learning C++ and I'm trying to generate a random integer thats either 1, 2, or 3. I searched around and all the examples I see of generating random numbers are confusing and always different from the last example I looked at. Is there a simple way to do this?

我刚开始学习 C++,我正在尝试生成一个随机整数,即 1、2 或 3。我四处搜索,我看到的所有生成随机数的示例都令人困惑,并且总是与我查看的最后一个示例不同。有没有一种简单的方法可以做到这一点?

回答by Joel Falcou

The modulo solution is the most straightforward but it usually loses randomness as modulo as the tendency to "eat up" the lowest bits of the result.

模数解是最直接的,但它通常会失去随机性,因为模数会“吃掉”结果的最低位。

A more random way is to map [0,1[ over [a,b[ in a linear way:

一种更随机的方法是以线性方式将 [0,1[ 映射到 [a,b[ ]:

int roll(int min, int max)
{
   // x is in [0,1[
   double x = rand()/static_cast<double>(RAND_MAX+1); 

   // [0,1[ * (max - min) + min is in [min,max[
   int that = min + static_cast<int>( x * (max - min) );

   return that;
}

A generic version is trivially derived from these to get a roll( T min, T max) version.

通用版本是从这些中简单地派生出来的,以获得 roll( T min, T max) 版本。

回答by Seth Carnegie

Try this:

尝试这个:

srand(time(NULL));

int randNum = (rand() % 3) + 1; // you don't need the (...) surrounding rand() % 3 but it helps for clarity

This works by taking the remainder of the return value of the randfunction divided by three (which can be 0, 1, or 2) and adding one (to come up with either 1, 2, or 3).

这是通过将rand函数返回值的余数除以三(可以是 0、1 或 2)并加一(得出 1、2 或 3)来实现的。

Make sure you include the cstdliband ctimeheaders.

确保包含cstdlibctime标题。

Also, call srandonly one time, not each time you generate a random number.

另外,srand只调用一次,而不是每次生成随机数时调用。

回答by lily

Write a C++ program to generate 25 random numbers in the range 0 to 50. You should group them into categories using queue. The example of the output program as shown below:

编写一个 C++ 程序来生成 0 到 50 范围内的 25 个随机数。您应该使用队列将它们分组。输出程序示例如下所示:

Output:

输出:

Welcome to a demonstration of categorizing data. We generate 25 random numbers and then group them into categories using queues.

欢迎观看数据分类演示。我们生成 25 个随机数,然后使用队列将它们分组。

Categorizing data: 24 7 31 23 26 14 19 8 9 6 43 16 22 0 39 46 22 38 41 23 19 18 14 3 41 End of data categorization

数据分类:24 7 31 23 26 14 19 8 9 6 43 16 22 0 39 46 22 38 41 23 19 18 14 3 41 数据分类结束

Data 0.. 9 : 7 8 9 6 0 3
Data 10..19 : 14 19 16 19 18 14
Data 20..29 : 24 23 26 22 22 23
Data over 29 : 31 43 39 46 38 41 41

数据 0.. 9 : 7 8 9 6 0 3
数据 10..19 : 14 19 16 19 18 14
数据 20..29 : 24 23 26 22 22 23
29 上的数据 : 31 43 38 416 3