在整个范围内均匀生成随机数 [英] Generate random numbers uniformly over an entire range

查看:366
本文介绍了在整个范围内均匀生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在指定的时间间隔内产生随机数[max; min]。



此外,随机数应均匀分布在区间

  

for(int i = 0; i <6; i ++)
{
DWORD random = rand()%(max-min + 1)+ min;
}

在我的测试中,只在一个点产生随机数。 >

 示例
min = 3604607;
max = 7654607;

产生的随机数字:

  3631594 
3609293
3630000
3628441
3636376
3621404

从以下答案:OK,RAND_MAX是32767.我在C ++ Windows平台上。是否有其他方法生成具有均匀分布的随机数?

解决方案

为什么 rand 是一个坏主意



您在这里得到的大多数答案都使用了 rand 函数,模运算符。该方法不能均匀生成数字(这取决于范围和<



C ++ 11并生成范围



使用C ++ 11多个其他选项上升。其中一个适合您的要求,在一个范围内生成一个随机数,相当不错: std :: uniform_int_distribution 。这里有一个例子:

  const int range_from = 0; 
const int range_to = 10;
std :: random_device rand_dev;
std :: mt19937 generator(rand_dev());
std :: uniform_int_distribution< int> distr(range_from,range_to);

std :: cout<< distr(generator)<< '\\\
';

这里是正在运行的示例。



其他随机生成器



=http://en.cppreference.com/w/cpp/numeric/random> < random> 头提供无数其他随机数生成器不同种类的分布,包括伯努利,泊松和正态。



如何洗牌一个容器?<​​/ h3>

提供 std :: random_shuffle ,可以用法如下:

  std :: vector< int> vec = {4,8,15,16,23,42}; 

std :: random_device random_dev;
std :: mt19937 generator(random_dev());

std :: shuffle(vec.begin(),vec.end(),generator);

算法会以线性复杂度随机重新排序元素。



Boost.Random



另一种方法是,如果你没有访问C ++ 11 +编译器, href =http://www.boost.org/doc/libs/1_55_0/doc/html/boost_random.html> Boost.Random 。它的接口非常类似于C ++ 11。


I need to generate random numbers within a specified interval, [max;min].

Also, the random numbers should be uniformly distributed over the interval, not located to a particular point.

Currenly I am generating as:

for(int i=0; i<6; i++)
{
    DWORD random = rand()%(max-min+1) + min;
}

From my tests, random numbers are generated around one point only.

Example
min = 3604607;
max = 7654607;

Random numbers generated:

3631594
3609293
3630000
3628441
3636376
3621404

From answers below: OK, RAND_MAX is 32767. I am on C++ Windows platform. Is there any other method to generate random numbers with a uniform distribution?

解决方案

Why rand is a bad idea

Most of the answers you got here make use of the rand function and the modulus operator. That method may not generate numbers uniformly (it depends on the range and the value of RAND_MAX), and is therefore discouraged.

C++11 and generation over a range

With C++11 multiple other options have risen. One of which fits your requirements, for generating a random number in a range, pretty nicely: std::uniform_int_distribution. Here's an example:

const int range_from  = 0;
const int range_to    = 10;
std::random_device                  rand_dev;
std::mt19937                        generator(rand_dev());
std::uniform_int_distribution<int>  distr(range_from, range_to);

std::cout << distr(generator) << '\n';

And here's the running example.

Other random generators

The <random> header offers innumerable other random number generators with different kind of distributions including Bernoulli, Poisson and normal.

How can I shuffle a container?

The standard provides std::random_shuffle, which can be used as follows:

std::vector<int> vec = {4, 8, 15, 16, 23, 42};

std::random_device random_dev;
std::mt19937       generator(random_dev());

std::shuffle(vec.begin(), vec.end(), generator);

The algorithm will reorder the elements randomly, with a linear complexity.

Boost.Random

Another alternative, in case you don't have access to a C++11+ compiler, is to use Boost.Random. Its interface is very similar to the C++11 one.

这篇关于在整个范围内均匀生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆