简体   繁体   English

如何在一个范围内生成随机数?

[英]How do I generate random numbers in a range?

I'm creating a program for school that works as follows: user thinks of a number and computer tries to guess it.我正在为学校创建一个程序,其工作方式如下:用户想到一个数字,计算机尝试猜测它。 computer generates a random number, asks user if number is higher or lower, then if the number is higher it takes that number and sets that as the minimum value for the random number range(ex. generates a 47, then will only generate numbers between 47-100).计算机生成一个随机数,询问用户数字是更高还是更低,然后如果数字更高,则取该数字并将其设置为随机数范围的最小值(例如,生成 47,则只会生成介于两者之间的数字) 47-100)。 if the number is lower it takes the random number that was generated and makes that the minimum value.如果数字较小,则采用生成的随机数并将其设为最小值。

My problem is with the random number generator and the range.我的问题是随机数生成器和范围。 I can generate the random number by seeding it at the start { srand(time(NULL));} then generate a number { rand_num = rand()%100;我可以通过在开始时播种来生成随机数 { srand(time(NULL));} 然后生成一个数字 { rand_num = rand()%100; } generates a number between 1-100. } 生成 1-100 之间的数字。 but I can't figure out how to generate a number in a range (example 47-89).但我不知道如何在一个范围内生成一个数字(例如 47-89)。 thought it was {rand_num = rand()%89 + 47}, but this doesn't seem to work.以为是 {rand_num = rand()%89 + 47},但这似乎不起作用。

Github: https://gist.github.com/EthanA2020/e121b27ab80fa6e2d32df1396e62b632 Github: https : //gist.github.com/EthanA2020/e121b27ab80fa6e2d32df1396e62b632

{

    srand(time(NULL));
    do {
    rand_num=rand()%47+89;
    cout << rand_num << endl;
    cout << "enter: "; cin >> userquitval;
    }while (userquitval!=7);
}

To generate a random number in C++11 or later, use the C++ random library.要在 C++11 或更高版本中生成随机数,请使用 C++ 随机库。 This should be much preferred over the C utilities for any application.对于任何应用程序,这应该比 C 实用程序更受欢迎。

#include <random>
#include <iostream>

int main() {
  std::random_device seed;
  std::mt19937 gen{seed()}; // seed the generator
  std::uniform_int_distribution dist{47, 89}; // set min and max
  int guess = dist(gen); // generate number
  std::cout << "Computer guess: " << guess << '\n';
}

Read the relevant cppreference page for more information.阅读相关的cppreference 页面以获取更多信息。

In the modulo operator you have to use the length of the interval you want the values in.在模运算符中,您必须使用您想要的值的间隔长度。

rand_num=rand()%(90-47)+47;

As mentioned by Jesper, this introduces a bias.正如 Jesper 所提到的,这会引入偏见。 To understand it, think what happens if you want a number equal to 0 or 1, and the range of rand is [0, 1, 2].为了理解它,想想如果你想要一个等于 0 或 1 的数字,并且 rand 的范围是 [0, 1, 2] 会发生什么。 With the above formula, the probability of getting a 0 would be 0.66 and the probability of getting a 1 would be 0.33).根据上述公式,得到 0 的概率为 0.66,得到 1 的概率为 0.33)。

To overcome this problem, you can use the following formula that scales the random number to a float in the [0,1] interval, and then use that to generate the number you need.为了克服这个问题,您可以使用以下公式将随机数缩放为 [0,1] 区间中的浮点数,然后使用它来生成您需要的数字。

(double) uniform = (double) rand() / (double) RAND_MAX;
rand_num = (int) ((90.0 - 47.0)*uniform) + 47);

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

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