m和n之间的rand() [英] rand() between m and n

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

问题描述






我需要帮助才能生成2到8之间的随机数。


#包括< cstdlib>

使用std :: rand;


以下超出我的范围,


int main(){


for(int i = 0; i< 50; i ++){

int x =(int(rand() )/ 444489786)* 8;

cout<< x<< ''\t''<<结束;

}

}


它可以是任意质量的随机数。

谢谢

解决方案

Gary Wessle< ph **** @ yahoo.comwrites:


我需要帮助才能生成2到8之间的随机数。



所以你需要3,4,5,6,7中的随机数字,对吗?


以下是超出我的范围,



你得到全零吗?


int main(){


for(int i = 0; i< 50; i ++){

int x =(int(rand())/ 444489786)* 8;

cout<< x<< ''\t''<<结束;

}

}


它可以是任何质量随机数。



因此rand函数应该足够了。


将rand函数的结果转换为float

允许您将rand结果标准化为

区间[0,1]作为浮点数。然后你可以用
乘以5(你想从

范围内的五个数字中选择一个数字,不是吗?)然后回合并且

为结果添加三个。


对于某些代码示例,请参阅例如

http://cplus.about.com/od/advancedtu.../aa041303c.htm
HTH

-

Marco Wahl
http://visenso.com



我需要帮助来生成一些随机数介于2和8之间。


#include< cstdlib>

使用std :: rand;


以下是超出我的范围,


int main(){


for(int i = 0; i< 50; i ++) {

int x =(int(rand())/ 444489786)* 8;

cout<< x<< ''\t''<<结束;

}

}



//返回随机数elm [from; upto]

int rnd_range(int from,int upto)

{

return(rand()%(upto - from + 1)) +来自;

}





" Gernot Frisch" < Me@Privacy.net写了留言

新闻:4j ************ @ individual.net ...


>


>我需要帮助才能生成2到8之间的随机数。

#include< cstdlib>
使用std :: rand;

以下内容超出了我的范围,

int main(){

for(int i = 0; i < 50; i ++){
int x =(int(rand())/ 444489786)* 8;
cout<< x<< ''\t''<< endl;
}
}



//返回随机数elm [from; upto]

int rnd_range(int from,int upto)

{

return(rand()%(upto - from + 1)) +来自;

}



这样做绝对没问题,但是另一种方法还是使用随机的

数字r out of [0,1)之间(x = Min + r *(Max-Min))是有利的,因为它并没有篡改随机性。发电机的生成顺序没有

影响。


干杯

克里斯



Hi

I need help to generate some random numbers between 2 and 8.

#include <cstdlib>
using std::rand;

the following was out of my range,

int main() {

for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << ''\t'' << endl;
}
}

it can be any quality random number.
thanks

解决方案

Gary Wessle <ph****@yahoo.comwrites:

I need help to generate some random numbers between 2 and 8.

So you need random numbers out of 3, 4, 5, 6, 7, right?

the following was out of my range,

Did you get all zeros?

int main() {

for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << ''\t'' << endl;
}
}

it can be any quality random number.

So the rand function should suffice.

Converting the result of the rand function to float
allows you to normalize the rand result into the
interval [0, 1] as floating point number. Then you can
multiply with five (you want to pick a number from a
range of five numbers, don''t you?) and then round and
add three to the result.

For some code example see e.g.

http://cplus.about.com/od/advancedtu.../aa041303c.htm
HTH
--
Marco Wahl
http://visenso.com


I need help to generate some random numbers between 2 and 8.

#include <cstdlib>
using std::rand;

the following was out of my range,

int main() {

for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << ''\t'' << endl;
}
}

// return random number elm[from; upto]
int rnd_range( int from, int upto)
{
return (rand() % (upto - from + 1)) + from;
}




"Gernot Frisch" <Me@Privacy.netwrote in message
news:4j************@individual.net...

>

>I need help to generate some random numbers between 2 and 8.

#include <cstdlib>
using std::rand;

the following was out of my range,

int main() {

for (int i = 0; i < 50; i++){
int x = (int(rand())/444489786)*8;
cout << x << ''\t'' << endl;
}
}


// return random number elm[from; upto]
int rnd_range( int from, int upto)
{
return (rand() % (upto - from + 1)) + from;
}

This works absolutely fine, but still the other approach using a random
number between r out of [0,1) ( x = Min + r * (Max-Min) ) is favorable
because it doesn''t tamper with the "randomness" of the generator and has no
influence on the period of the generated sequence.

Cheers
Chris


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

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