随机整数从0到999 [英] Random Integers from 0 to 999

查看:70
本文介绍了随机整数从0到999的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人曾在clc上发布以下宏:


#define randint(a,b)(a)+(((b) - (a)+1)*(漂浮)rand()/ RAND_MAX)


不幸的是它有缺陷。如果rand()返回RAND_MAX,结果可以比b大一个



那么有人可以提供一个*正确的*宏(或函数)返回a

一系列值之间(实际上是)的随机整数?例如

randint(0,999)可以返回:


0

10

777

999

Mike

Someone once posted the following macro on clc:

#define randint(a,b) (a)+(((b)-(a)+1)*(float)rand()/RAND_MAX)

Unfortunately it''s flawed. If rand() returns RAND_MAX the result can be
one larger than b.

So can someone provide a *proper* macro (or function) that returns a
random integer between (actually in) a range of values? For example
randint(0, 999) could return:

0
10
777
999

Mike

推荐答案

Michael B Allen写道:
Michael B Allen wrote:
有人曾在clc上发布以下宏:

#define randint(a,b)(a)+(((b) - (a)+1)*(浮点数) )rand()/ RAND_MAX)

不幸的是它有缺陷。如果rand()返回RAND_MAX,结果可能比b大一个。

那么有人可以提供一个*正确的*宏(或函数),它返回一个
随机整数(实际上是)一系列的价值观?例如
randint(0,999)可以返回:

0
10
777
999
Someone once posted the following macro on clc:

#define randint(a,b) (a)+(((b)-(a)+1)*(float)rand()/RAND_MAX)

Unfortunately it''s flawed. If rand() returns RAND_MAX the result can be
one larger than b.

So can someone provide a *proper* macro (or function) that returns a
random integer between (actually in) a range of values? For example
randint(0, 999) could return:

0
10
777
999



你的射击在哪里?没有归属,没有人知道

来自哪个来源。


#define RANDINT(a,b)(a)+(((b) - ( a)+1)*(双)rand()/(1u + RAND_MAX))


可能会为您提供更好的服务;请注意,如果RAND_MAX的数字超过浮点数可以表示的数字,那么很好地为你服务



此方法存在其他缺陷;为什么不给你

使用劳伦斯柯比(或其他常客)的建议

哪些价值同等可能?

除此之外:我宁愿为此目的使用一个函数。

如果您需要许多随机数,请考虑填充一个数组并从那里检索
直到它为用完了,然后再补充。

干杯

迈克尔

-

电子邮箱:我的是an / at / gmx / dot / de address。



Where is your shot at it? Without attribution, nobody knows
from which source this comes.

#define RANDINT(a,b) (a)+(((b)-(a)+1)*(double)rand()/(1u+RAND_MAX))

may serve you better; note the double which serves you well
if RAND_MAX has more digits than float can represent.
There are other deficiencies for this approach; why don''t you
use the suggestions of, say, Lawrence Kirby (or other regulars)
which make all values equally probable?
Apart from that: I would rather use a function for this purpose.
If you need many random numbers, consider filling an array and
retrieving from there until it is "used up", then refilling.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Michael B Allen写道:
Michael B Allen wrote:
有人曾经发布过以下宏clc:

#define randint(a,b)(a)+(((b) - (a)+1)*(float)rand()/ RAND_MAX)

那么有人可以提供一个*正确的*宏(或函数),它返回一个
随机整数(实际上是)一系列的价值观?例如
randint(0,999)可以返回:

0
10
777
999
Someone once posted the following macro on clc:

#define randint(a,b) (a)+(((b)-(a)+1)*(float)rand()/RAND_MAX)

Unfortunately it''s flawed. If rand() returns RAND_MAX the result can be
one larger than b.

So can someone provide a *proper* macro (or function) that returns a
random integer between (actually in) a range of values? For example
randint(0, 999) could return:

0
10
777
999



int randint(int min,int max)

{

断言(min< = max);

返回min + rand()%(max - min + 1);

}


0< = rand()< = RAND_MAX

0< = rand()%(max-min + 1)< max-min + 1

0< = rand()%(max-min + 1)< = max-min

min< = min + rand( )%(max-min + 1)< = max



int randint(int min, int max)
{
assert(min <= max);
return min + rand() % (max - min + 1);
}

0 <= rand() <= RAND_MAX
0 <= rand()%(max-min+1) < max-min+1
0 <= rand()%(max-min+1) <= max-min
min <= min+rand()%(max-min+1) <= max


Michael Mair写道:
Michael Mair wrote:
Michael B Allen写道:
Michael B Allen wrote:
有人曾在clc上发布以下宏:

#define randint(a,b)(a)+(((b) - (a)+1)*(float)rand( )/ RAND_MAX)

不幸的是它有缺陷。如果rand()返回RAND_MAX,结果
可以比b大一个。
Someone once posted the following macro on clc:

#define randint(a,b) (a)+(((b)-(a)+1)*(float)rand()/RAND_MAX)

Unfortunately it''s flawed. If rand() returns RAND_MAX the result can be one larger than b.



你在哪里拍摄?没有归属,没有人知道
来自哪个来源。

#define RANDINT(a,b)(a)+(((b) - (a)+1)*(double )rand()/(1u + RAND_MAX))

可以为你提供更好的服务;请注意,如果RAND_MAX的数字比浮点数多,则可以很好地为你服务

这种方法还有其他不足之处。为什么你不使用劳伦斯柯比(或其他常客)的建议,这些建议使所有价值观同样可能?
除此之外:我宁愿使用一个函数这个目的。
如果您需要许多随机数,请考虑填充一个数组并从那里检索,直到它用完,然后重新填充。



Where is your shot at it? Without attribution, nobody knows
from which source this comes.

#define RANDINT(a,b) (a)+(((b)-(a)+1)*(double)rand()/(1u+RAND_MAX))

may serve you better; note the double which serves you well
if RAND_MAX has more digits than float can represent.
There are other deficiencies for this approach; why don''t you
use the suggestions of, say, Lawrence Kirby (or other regulars)
which make all values equally probable?
Apart from that: I would rather use a function for this purpose.
If you need many random numbers, consider filling an array and
retrieving from there until it is "used up", then refilling.




不幸的是,当RAND_MAX恰好等于INT_MAX时,你和Grumble'的片段产生UB由于整数

溢出(就像在我所有的16位上一样)
编译器)和参数是(0,RAND_MAX)。看起来好像


#define RANDINT(a,b)\

((b) - (a)== RAND_MAX?(a)+ rand ():( a)+ rand()%((b) - (a)+1))


会做到这一点,虽然分布很糟糕。再说一次,对于

嵌入式架构,其中浮点数和RAM都不是

选项,我使用这些生成器完全是因为它们的简单而不是

随机性。我的大多数测试数据只需要比b / b $ b $迭代更好,但不是真正随机的。在哪里真实我需要随机性

并问大男孩(数学家);统一分布不会

无论如何都会削减它。


Mark



Unluckily, both, your and Grumble''s snippet produce UB due to integer
overflow when RAND_MAX happens to equal INT_MAX (like on all my 16-Bit
compilers) and the arguments are (0,RAND_MAX). It looks like

#define RANDINT(a,b)\
((b)-(a)==RAND_MAX?(a)+rand():(a)+rand()%((b)-(a)+1))

will do it, although the distribution will be abysmal. Then again, for
embedded architectures, where neither floating point nor much RAM is an
option, I use such generators exactly for their simplicity and not the
randomness. Most of my test data just needs to be better than
iterative, but not truly random. Where "real" randomness is needed I go
and ask the big boys (the mathematicians); uniform distributions won''t
cut it most of the time anyway.

Mark

这篇关于随机整数从0到999的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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