RAND介于0和1之间 [英] RAND between 0 and 1

查看:111
本文介绍了RAND介于0和1之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我觉得这已经多次发布..但我尝试了几个食谱

获取[0之间的随机数] 1],但我不能让它们工作。


那么,有没有人知道如何生成随机数x,0 <= x< = 1 ??


谢谢,


FBM


这就是我得到的...... 。


浮动rndm =(浮动)(rand()/ RAND_MAX);


我也试过

浮动rndm =(浮动)(rand()%2);


没有成功...

Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can''t make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??

Thanks,

FBM

That''s what I got.......

float rndm = (float) (rand() / RAND_MAX);

and I tried also

float rndm = (float) (rand() % 2);

with no success...

推荐答案

Fernando Barsoba写道:
Fernando Barsoba wrote:
我认为这已被多次发布..但我尝试了几个食谱
获取[0,1]之间的随机数但是我不能让它们工作。

那么,有没有人知道如何生成随机数x,对于0< = x< = 1 ??
I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can''t make them work.

So, does anyone know how to generate random number x, for 0<= x <=1??



FAQ中的问题13.16: http://www.eskimo.com/~scs/C-faq/q13.16.html

还可以查看附近的问题。


S.


Question 13.16 in the FAQ: http://www.eskimo.com/~scs/C-faq/q13.16.html.
Also check out the questions in the vicinity.

S.


On Thu ,2005年9月29日03:51:13 GMT,Fernando Barsoba

< fb ****** @ verizon.net>在comp.lang.c中写道:
On Thu, 29 Sep 2005 03:51:13 GMT, Fernando Barsoba
<fb******@verizon.net> wrote in comp.lang.c:
大家好,

我觉得这已经多次发布..但是我尝试了几个食谱
获得[0,1]之间的随机数,但我不能让它们起作用。


定义你现在工作的意思。

那么,有没有人知道如何生成随机数x,0< = x< = 1? ?

谢谢,

FBM

这就是我得到的.......

float rndm =(float)(rand()/ RAND_MAX);


啊哈!整数师! RAND_MAX是一个宏,它的计算结果为int类型的数字文字。 rand()返回一个类型为

int的值。将int除以int的结果是一个int商,

任何余数都被丢弃。在

整数除法/截断之后将结果转换为浮点数不会使小数部分返回。

我也试过了

浮动rndm =(浮动)(rand()%2);

没有成功...
Hi all,

I think this has been posted many times.. but I tried several recipes
for obtaining a random number between [0,1], but I can''t make them work.
Define what you mean by now working.
So, does anyone know how to generate random number x, for 0<= x <=1??

Thanks,

FBM

That''s what I got.......

float rndm = (float) (rand() / RAND_MAX);
Aha! Integer division! RAND_MAX is a macro that evaluates to a
numeric literal of type int. rand() returns a value that is of type
int. The result of dividing an int by an int is an int quotient, with
any remainder discarded. Casting the result to a float after the
integer division/truncation does not bring the fractional part back.
and I tried also

float rndm = (float) (rand() % 2);

with no success...




将来不要只是说不起作用。描述你得到的结果

。在这种情况下它没有任何区别,但是当有人试图找出你的问题的原因时,它经常会发生这种情况。


无论如何,你需要将

分区的一个或另一个操作数转换为浮点数。这将导致另一个操作数

升级为浮点数。然后将对两个float

值执行浮动除法,从而产生浮点结果。在任何情况下,你都不需要将

转换为浮点数,转换是自动的,并且施法操作员绝对没有做任何事情。


浮动rndm = rand()/(浮动)RAND_MAX;


-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



In the future, don''t just say "doesn''t work". Describe the results
you are getting. It makes no difference in this case, but very often
it does when someone is trying to locate the cause of your problem.

In any case, you need to cast one or the other of the operands of the
division to a float. This will cause promotion of the other operand
to a float. Then a float division will be performed on the two float
values, yielding a float result. In any case, you do not need the
cast on the assignment to a float, that conversion is automatic and
the cast operator does absolutely nothing.

float rndm = rand()/(float)RAND_MAX;

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html




" Fernando Barsoba" < FB ****** @ verizon.net>写了

"Fernando Barsoba" <fb******@verizon.net> wrote
float rndm =(float)(rand()/ RAND_MAX);
float rndm = (float) (rand() / RAND_MAX);



这是这样做的方法。但是你需要首先投射到浮点数,

否则你会得到一个整数除法。

顺便提一下,传统的除以RAND_MAX加一,因为很多当0-1

区间的随机数恰好为1时,
算法不能很好地工作。


This is the way to do it. However you need to cast to a float first,
otherwise you will get an integer division.
Incidentally it is traditional to divide by RAND_MAX plus one, because a lot
of algorithms don''t work as nicely when a random number on the interval 0-1
is exactly unity.


这篇关于RAND介于0和1之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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