生成set [0,3]的随机整数 [英] Generate a random integer of set [0,3]
问题描述
我需要随机选择程序中的四条路径之一。使用我知道的
工具,我能想到的最好方法是做一些事情
如下:
// ==============================
#include< stdlib.h> //允许rand()函数
int i;
i = rand(); // i =任意数集[0,2 ^ 32 -1]或[0,4294967295]
if(0< = i<(4294967295/4))
{/ *选择一个* /}
否则如果(4294967295/4< = i<(4294967295/2))
{/ *选择两个* /}
否则如果(4294967295/2< = i< 3 *(4294967295/4))
{/ * choice three * /} >
else if(3 * 4294967295< = i< = 4294967295)
{/ * choice four * /}
// ====== ========================
对于一项简单的任务来说,这似乎很多。是否有一些函数
沿着类似的行:
rand(3);
哪会产生一个随机整数的集合[0, 3]?
-
Toby
Toby Newman写道:
我需要在程序中随机选择四个路径中的一个。使用我所知道的
工具,我能想到的最好的方法是做一些像以下那样的事情:
// ========= =====================
#include< stdlib.h> //允许rand()函数
int i;
i = rand(); // i =任意数量的集[0,2 ^ 32 -1]或[0,4294967295]
if(0< = i<(4294967295/4))
{ / *选择一个* /}
否则如果(4294967295/4< = i<(4294967295/2))
{/ *选择两个* /}
否则if(4294967295/2< = i< 3 *(4294967295/4))
{/ *选择三* /}
如果(3 * 4294967295< = i< = 4294967295)
{/ *选择四* /}
// ==============================
这似乎是一个很多工作都是为了一项简单的任务。是否有一些功能
沿着类似的路线:
rand(3);
哪会产生一组[0,3]的随机整数?
查看clc faq以获得答案:
http://www.eskimo.com/~scs/C-faq/q13.16.html
-
boa @ home
Toby Newman在10/08/04写道:这会生成一个随机整数设置[0,3]?
阅读常见问题解答怎么样?
-
Emmanuel
C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
C是一个敏锐的工具
#Emmanuel DelahayeToby Newman在10/08/04写道:其中会生成一组[0,3]的随机整数吗?
阅读常见问题解答怎么样?
提示未来:要使C FAQ可搜索,请使用谷歌搜索
:
关键字站点: www.eskimo.com
例如我找到了问题的答案:
随机网站: www.eskimo.com
:)
-
Toby
I need to randomly choose one of four paths in my program. Using the
tools I know, the best way I can think to do it is by doing something
like the following:
//==============================
#include <stdlib.h> //allow rand() function
int i;
i = rand (); // i = any number of the set [0,2^32 -1] or [0, 4294967295]
if (0<=i<(4294967295/4))
{ /* choice one*/ }
else if (4294967295/4<=i<(4294967295/2))
{ /* choice two*/ }
else if (4294967295/2<=i<3*(4294967295/4))
{ /* choice three*/ }
else if (3*4294967295<=i<=4294967295)
{ /* choice four*/ }
//==============================
This seems like a lot of work for a simple task. Is there some function
along similar lines to:
rand(3);
Which would generate a random integer of the set [0,3] ?
--
Toby
Toby Newman wrote:
I need to randomly choose one of four paths in my program. Using the
tools I know, the best way I can think to do it is by doing something
like the following:
//==============================
#include <stdlib.h> //allow rand() function
int i;
i = rand (); // i = any number of the set [0,2^32 -1] or [0, 4294967295]
if (0<=i<(4294967295/4))
{ /* choice one*/ }
else if (4294967295/4<=i<(4294967295/2))
{ /* choice two*/ }
else if (4294967295/2<=i<3*(4294967295/4))
{ /* choice three*/ }
else if (3*4294967295<=i<=4294967295)
{ /* choice four*/ }
//==============================
This seems like a lot of work for a simple task. Is there some function
along similar lines to:
rand(3);
Which would generate a random integer of the set [0,3] ?
Check out the clc faq for an answer:
http://www.eskimo.com/~scs/C-faq/q13.16.html
--
boa@home
Toby Newman wrote on 10/08/04 :Which would generate a random integer of the set [0,3] ?
What about reading the FAQ ?
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
# Emmanuel DelahayeToby Newman wrote on 10/08/04 :Which would generate a random integer of the set [0,3] ?
What about reading the FAQ ?
Tip for the future: To make the C FAQ searchable, use google, and search
for:
keyword site:www.eskimo.com
e.g. I found the answer to my question by:
random site:www.eskimo.com
:)
--
Toby
这篇关于生成set [0,3]的随机整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!