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
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01