本文介绍了C ++随机数从1到非常大的数(例如2500万)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经考虑过使用 rand() 但我是正确的,认为最大数量, RAND_MAX is = 32000(有关于)?



有没有办法解决这个问题,一种方法不会降低选择非常低的数字的概率,也不会增加挑选高/中等数字的概率?



编辑:@Jamey D的方法完全独立于Qt。

解决方案

新的C ++ 11 std :: uniform_real_distribution

  #include< ; random> 

std :: random_device rd;
std :: mt19937 gen(rd());

std :: uniform_real_distribution<>分布(1,25000000);

//生成一个随机整数:
double random = distribution(gen);


How would you make a function that generates a random number from 1 to 25 million?

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?

Edit: @Jamey D 's method worked perfectly independent of Qt.

解决方案

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万)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 14:58