是否RAND_MAX的值总是(2 ^ n)-1? [英] Is the value of RAND_MAX always (2^n)-1?

查看:149
本文介绍了是否RAND_MAX的值总是(2 ^ n)-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++感兴趣,虽然我怀疑只是导入C标准定义。



如果RAND_MAX总是(2 ^ n),那么, 1,这简化了处理最近移动代码从MinGW GCC到Linux GCC的问题。 RAND_MAX似乎更大(我没有检查,但可能等于INT_MAX或任何符号是什么),所以一些老天真写的RAND_MAX是不是大的足够 - 所以工作的代码反向。现在我需要决定我需要这个库的一般程度,考虑到编写代码的准确性,正确处理溢出的可能性,而不做假设。一个整数的宽度。



无论如何,有没有合理广泛使用的C ++编译器,使用除了(2 ^ n)-1之外的RAND_MAX?

此外,我要纠正((RAND_MAX |(RAND_MAX >> 1))== RAND_MAX)总是且仅当RAND_MAX等于((2 ^ n)-1时为真)对于一些无符号整数n。我相信RAND_MAX在技术上是一个int,但它没有任何意义,有一个负值或分数值,所以我想我可以安全地折扣那些。 Bit-fiddling通常不会打扰我,但我一直认为表达式看起来错了,我不知道为什么。



最后,虽然我不是要快乐,直到我有自己的工作解决方案,应该用于随机数,而不是自己写?我需要在0≤x≤1范围内的随机数。参数,并且我特别想要所有数字的as-equal-as-sanely-possible概率。例如,取(rand()%upperbound)对较小的值给出偏向,特别是当上限很大时 - 我想避免这种情况。



或C ++ 0x的东西?



EDIT



页面一侧的相关位显示确实有一种方法可以获得带有给定的boost和boost的下限和上限的随机数。

解决方案

我不知道对 RAND_MAX 的保证是什么,但你最好避免它,如果可能的话,因为破坏的实现的数量,在今天的应用中开始循环很快。 此处介绍了统一分发方式。



我建议您改用 Boost.Random Mersenne twister 生成器代表了速度,内存使用和质量。


I'm interested for C++, though I suspect that simply imports the C standard definition. I believe the answer is no for what the standard says, but I'm most interested in the in-practice answer.

If RAND_MAX is always (2^n)-1, that simplifies dealing with an issue that turned up recently moving code from MinGW GCC to Linux GCC. RAND_MAX seems to be bigger (I didn't check, but possibly equal to INT_MAX or whatever the symbol is), so some old naively written RAND_MAX-isn't-big-enough-so-work-around-it code backfired. Now I need to decide just how general I need this library to be, considering the fiddliness of writing code that copes correctly with the possibility of overflow without making assumptions about e.g. the width of an int.

Anyway, are there any reasonably widely used C++ compilers that use something other than (2^n)-1 for RAND_MAX?

Also, am I correct that ((RAND_MAX | (RAND_MAX >> 1)) == RAND_MAX) is always and only true if RAND_MAX is equal to ((2^n)-1) for some unsigned integer n. I believe RAND_MAX is technically an int, but it makes no sense to have a negative or fractional value, so I think I can safely discount those. Bit-fiddling doesn't normally bother me, but I keep thinking the expression looks wrong, and I can't figure out why.

Finally, although I'm not going to be happy until I've got a working solution of my own, what should I be using for random numbers rather than write it myself? I need random numbers in the range 0 <= x < parameter, and I especially want as-equal-as-sanely-possible probabilities for all numbers. For example, taking (rand() % upperbound) gives a bias towards smaller values, especially when the upperbound is large - I want to avoid that.

Is there a Boost or C++0x thing for that?

EDIT

Following something in the "Related" bit on the side of the page shows there is indeed a way to get random numbers with given lower and upper bounds in boost.

解决方案

I don't know what the guarantees on RAND_MAX are, but you'd better avoid it if possible because of the number of broken implementations around and because it starts cycling quite quickly in today's applications. Getting a uniform distribution is described here.

I recommend Boost.Random instead. The Mersenne twister generator represents a good tradeoff between speed, memory use and quality.

这篇关于是否RAND_MAX的值总是(2 ^ n)-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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