写生成给定的特定范围之一的随机数,或一对随机数或随机数的三重一个c函数 [英] write a c function that generates one random number, or a pair of random numbers, or a triplet of random numbers given the particular ranges

查看:103
本文介绍了写生成给定的特定范围之一的随机数,或一对随机数或随机数的三重一个c函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须产生3种不同的情况下,随机数。
一世。 1骰子
II。一对骰子
三。 3骰子
我的问题:
1.请建议我SM良好的逻辑来为所有3例随机数。
2.做逻辑的变化,当我考虑2骰子的自定义搜索引擎,而不是1?
3.how多大的效果呢,使我们不得不genrate一个随机数影响随机函数的逻辑范围是多少?

i have to generate random numbers for 3 different cases. i. 1 dice ii. a pair of dice iii. 3 dices my questions: 1. please suggest me sm good logic to generate random numbers for all the 3 cases. 2. does the logic change when i consider the cses of 2 dices, rather than 1? 3.how much of an effect does the range in which we have to genrate a random number affect the logic of the random function?

推荐答案

如果范围足够小,你不应该有在使用通常的取模方式的问题。

If the range is small enough, you shouldn't have problems in using the usual modulo method

int GetRandomInt(int Min, int Max)
{
    return (rand()%(Max-Min+1))+Min;
}

(其中最小 A 最高指定一个闭区间[最小最高])

(where Min a Max specify a closed interval, [Min, Max])

和调用它一次为每个骰子。不要忘了叫函数srand(时间(NULL))在应用程序的开始(在开始的只有的,不是每次你想得到一个随机数)的种子的随机数发生器。

and calling it once for each dice roll. Don't forget to call srand(time(NULL)) at the start of the application (at the start only, not each time you want to get a random number) to seed the random number generator.

如果范围开始更大,你可能要面对两个问题:

If the range starts to be bigger, you may have to face two problems:

首先,)的明显的范围兰特(不是[0,+∞),取而代之的则是[0, RAND_MAX ],其中 RAND_MAX 的#define 保证至少32767如果你的范围(最大最小)跨越了 RAND_MAX ,然后,用这种方法,你就会有一些数字,将有一个零被返回的可能性。

First, the range of rand() obviously isn't [0, +∞), instead it's [0, RAND_MAX], where RAND_MAX is a #define guaranteed to be at least 32767. If your range (Max-Min) spans over RAND_MAX, then, with this method, you'll have some numbers that will have a zero probability of being returned.

这是更加微妙:假设 RAND_MAX 比你的范围更大,但不是的的更大,让我们说, RAND_MAX == 1.5 * /(最大值 - 最小值)
在这种情况下,结果分布不会均匀:兰特()返回范围为[0的整数, RAND_MAX (在此范围内的每个整数应该是等概率的),但你正在服用的分裂的其余与(最大值 - 最小值)。这意味着,在您要求的范围内上半年的数字有比其他人返回两次的概率:他们居然可以从第一的的第三个三分 RAND()的范围,而要求的范围内下半年可以从兰特的第二个三分之一()范围只来。

This is more subtle: suppose that RAND_MAX is bigger than your range, but not that bigger, let's say that RAND_MAX==1.5*/(Max-Min). In this case, the distribution of results won't be uniform: rand() returns you an integer in the range [0, RAND_MAX] (and each integer in this range should be equiprobable), but you are taking the rest of the division with (Max-Min). This means that the numbers in the first half of your required range have twice the probability of being returned than the others: they can actually come from the first and the third third of the rand() range, while the second half of the required range can come only from the second third of the rand() range.

这是什么意思你?

也许什么都没有。如果你想要做的是一个骰子滚模拟器,你可以不与取模方式问题,因为所涉及的范围小,而第二个问题,尽管是的还是的present,这几乎是无关紧要的:假设你的范围是3和 MAX_RAND 32767:从0到32765,0,1和2具有相同的概率,但马上要到32767 0和1增益一个潜在的出口,这几乎是不相关的,因为它们从一个完美的1/3通(三万二千七百六十六分之一万零九百二十二= 0333 ...)的概率为每一个到一个三万二千七百六十七分之一万零九百二十二2(〜0,33332)和32767分之10923( 〜0,33335)为0和1(假设兰特()提供的完美的分布)。

Probably nothing. If all you want to do is a dice-roll simulator, you can go without problems with the modulo method, since the range involved is small, and the second problem, despite being still present, it's almost irrelevant: suppose your range is 3 and MAX_RAND 32767: from 0 to 32765, 0, 1 and 2 has the same probability, but going up to 32767 0 and 1 gain one potential exit, which is almost irrelevant, since they pass from a perfect 1/3 (10922/32766=0,333...) probability for each one to a 10922/32767 for 2 (~0,33332) and 10923/32767 (~0,33335) for 0 and 1 (assuming that rand() provides a perfect distribution).

总之,克服这些问题一个比较采用的方法是伸展的兰特()可在更宽的范围内(或COM $ P $它pssing到使用方法类似这样的小范围):

Anyhow, to overcome such problems a quite used method is to "stretch" the rand() range over a wider range (or compressing it to a smaller range) using a method like this:

int GetRandomInt(int Min, int Max)
{
    return (int)(((double)rand())/MAX_RAND*(Max-Min))+Min;
}

根据等价兰特():MAX_RAND = X(最大值 - 最小值)。两倍的转换是必要的,之间兰特(),否则整数除法,其最大的价值总是在罕见的情况下,产生出0(或1 RAND()== MAX_RAND );它可以在整数运算首先执行的产品如果MAX_RAND小,范围太不太宽来完成,否则有溢出的危险性高。

based on the equivalence rand():MAX_RAND=X:(Max-Min). The conversion to double is necessary, otherwise the integer division between rand() and its maximum value will always yield 0 (or 1 in the rare case of rand()==MAX_RAND); it could be done in integer arithmetic performing the product first if MAX_RAND is small and the range too is not too wide, otherwise there's a high risk of overflow.

我怀疑,如果输出范围比)的范围兰特(大中,延伸和FP值截断(由于转换为int以某种方式分配)影响,但只是局部(例如,在小范围内,你可能永远不会得到一定的数量,但在全球的分布看起来OK)。

I suspect that, if the output range is bigger than the range of rand(), the "stretching" and the fp value truncation (due to the conversion to int) affect in some way the distribution, but just locally (e.g. in small ranges you may never get a certain number, but globally the distribution will look ok).

请注意,这种方法有助于克服对C标准库随机数生成器的扩散限制,即返回值的低位的低随机性 - 这是顺便说一下,当你执行你使用的是那些一模操作用小的输出范围。

Notice that this method helps to overcome to a diffused limitation of the C standard library random number generator, i.e. the low randomness of the lower bits of the returned value - which are, incidentally, the ones you are using when you perform a modulo operation with a small output range.

但是,请记住,C标准库RNG是一个简单的,即努力遵守易的统计规则,因此很容易predictable;当需要严重的随机数(例如,加密)它不应该被使用。对于这样的需求,有专门的RNG库(如 RNG部分在GNU科学图书馆),或者,如果你需要的真正的随机的东西,有几个的真正的随机数服务(最有名的是<一个HREF =htt​​p://random.irb.hr/相对=nofollow称号=量子随机位发生器服务>这个),不使用数学伪RNG,但是从拿他们的数字真随机源(如放射性衰变)。

However, keep in mind that the C standard library RNG is a simple one, that strives to comply with "easy" statistical rules, and as such is easily predictable; it shouldn't be used when "serious" random numbers are required (e.g. cryptography). For such needs there are dedicated RNG libraries (e.g. the RNG part of the GNU Scientific Library), or, if you need really random stuff, there are several real random number services (one of the most famous is this), which do not use mathematical pseudo-RNG, but take their numbers from real random sources (e.g. radioactive decay).

这篇关于写生成给定的特定范围之一的随机数,或一对随机数或随机数的三重一个c函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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