C++ Random number from 1 to a very large number (e.g. 25 million)(C++ 随机数从 1 到一个非常大的数字(例如 2500 万))
问题描述
你将如何制作一个生成 1 到 2500 万随机数的函数?
How would you make a function that generates a random number from 1 to 25 million?
我考虑过使用 rand() 但我认为最大数字 RAND_MAX 是 = 32000(大约)是正确的吗?
I've thought about using rand() but am I right in thinking that the maximum number, RAND_MAX is = 32000 (there about)?
有没有办法解决这个问题,一种不会降低选择非常低数字的概率并且不会增加选择高/中等数字的概率的方法?
Is there a way around this, a way that doesn't reduce the probability of picking very low numbers and doesn't increase the probability of picking high / medium numbers?
@Jamey D 的方法完全独立于 Qt.
@Jamey D 's method worked perfectly independent of Qt.
推荐答案
你可以(应该)使用新的 C++11 std::uniform_real_distribution
You could (should) use the new C++11 std::uniform_real_distribution
#include <random>
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> distribution(1, 25000000);
//generating a random integer:
double random = distribution(gen);
这篇关于C++ 随机数从 1 到一个非常大的数字(例如 2500 万)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 随机数从 1 到一个非常大的数字(例如 2500 万)
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
