生成范围内的随机数? [英] Generate a random number within range?

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

问题描述

可能的重复:
在 Objective-C 中生成随机数

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

How do I generate a random number which is within a range?

推荐答案

这实际上比大多数人意识到的要难一些:

This is actually a bit harder to get really correct than most people realize:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
 */

    int divisor = RAND_MAX/(limit+1);
    int retval;

    do { 
        retval = rand() / divisor;
    } while (retval > limit);

    return retval;
}

尝试仅使用 %(或等效地,/)获取范围内的数字几乎不可避免地会引入偏斜(即,某些数字将更频繁地生成比其他人).

Attempts that just use % (or, equivalently, /) to get the numbers in a range almost inevitably introduce skew (i.e., some numbers will be generated more often than others).

至于为什么使用%会产生偏斜的结果:除非你想要的范围是RAND_MAX的除数,偏斜是不可避免的.如果您从小数字开始,很容易看出原因.考虑拿 10 块糖果(我们假设你不能把它们切成小块)并试着把它平均分给三个孩子.显然这是不可能的——如果你把所有的糖果都分发出去,你能得到的最接近的结果是两个孩子得到三块糖果,其中一个得到四块.

As to why using % produces skewed results: unless the range you want is a divisor of RAND_MAX, skew is inevitable. If you start with small numbers, it's pretty easy to see why. Consider taking 10 pieces of candy (that we'll assume you can't cut, break, etc. into smaller pieces) and trying to divide it evenly between three children. Clearly it can't be done--if you hand out all the candy, the closest you can get is for two kids to get three pieces of candy, and one of them getting four.

让所有孩子得到相同数量的糖果只有一种方法:确保你根本不分发最后一块糖果.

There's only one way for all the kids to get the same number of pieces of candy: make sure you don't hand out the last piece of candy at all.

为了将其与上面的代码联系起来,让我们从 1 到 10 给糖果编号,从 1 到 3 给孩子编号.最初的除法表示,因为有三个孩子,我们的除数是 3.然后我们从桶中随机取出一颗糖果,查看它的数量并除以 3 并将其交给那个孩子——但如果结果大于 3(即我们选择了 10 号糖果),我们只是不把它分发出去——我们把它扔掉,然后再挑选一颗糖果.

To relate this to the code above, let's start by numbering the candies from 1 to 10 and the kids from 1 to 3. The initial division says since there are three kids, our divisor is three. We then pull a random candy from the bucket, look at its number and divide by three and hand it to that kid -- but if the result is greater than 3 (i.e. we've picked out candy number 10) we just don't hand it out at all -- we discard it and pick out another candy.

当然,如果您使用的是 C++ 的现代实现(即支持 C++11 或更新版本的实现),您通常应该使用标准库中的 distribution 类.上面的代码与 std::uniform_int_distribution 最接近,但标准库还包括 uniform_real_distribution 以及许多非均匀分布的类(伯努利、泊松、正常,可能还有其他几个我现在不记得了).

Of course, if you're using a modern implementation of C++ (i.e., one that supports C++11 or newer), you should usually use one the distribution classes from the standard library. The code above corresponds most closely with std::uniform_int_distribution, but the standard library also includes uniform_real_distribution as well as classes for a number of non-uniform distributions (Bernoulli, Poisson, normal, maybe a couple others I don't remember at the moment).

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

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