N组具有不同范围的随机数 [英] N groups of random numbers with different ranges

查看:60
本文介绍了N组具有不同范围的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在一个循环中在

不同范围之间生成不同的随机数。我尝试用以下方式解决

方式,但它总是描述相同的值。我正在寻找

这方面的建议。


#include< cstdlib>

#include< time.h> ;

#include< iostream.h>


int main()

{

int i,j;

for(i = 0; i< n; i ++)// N组rendom数量

{

j = randomize (1,5); //第一个数字

cout<< j;

j = randomize(1,6); //第二个数字等等..

cout<< j;

}

}


int randomize(int LOW,int HIGH)

{

int value;

time_t seconds; //声明变量以保持秒数在

//时钟。

时间(&秒); //从系统时钟获取值并放入

//秒变量。

srand((unsigned int)seconds); //将秒转换为

//无符号整数。

value = rand() %(HIGH - LOW + 1)+ LOW; //获取随机

//介于LOW和HIGH之间的值

返回值;

}

解决方案

Masud< Go ********** @ gmail.comwrites:
< blockquote class =post_quotes>
用于测试我想在单个循环中在

不同范围之间生成不同的随机数。我尝试用以下方式解决

方式,但它总是描述相同的值。我正在寻找

这方面的建议。


#include< cstdlib>



制作''#include< stdlib.h>''。


#include< ; time.h>

#include< iostream.h>



错误的语言;那是'C ++。使用''#include< stdio.h>''。


int main()

{

int i,j;

for(i = 0; i< n; i ++)// N组rendom number



那里没有''n'的声明。这显然不是你真正的代码。


{

j = randomize(1,5); //第一个数字



尚未声明''randomize''。你可以用这个来获得

,但你应该在调用

之前声明函数。在这种情况下,你可以将随机化的定义移动到main的定义之上。


cout<< j;

j = randomize(1,6); //第二个数字等等..

cout<< j;

}

}


int randomize(int LOW,int HIGH)

{

int value;

time_t seconds; //声明变量以保持秒数在

//时钟。

时间(&秒); //从系统时钟获取值并放入

//秒变量。

srand((unsigned int)seconds); //将秒转换为

//无符号整数。

value = rand() %(HIGH - LOW + 1)+ LOW; //获取随机

//介于LOW和HIGH之间的值

返回值;

}



除非你有特殊原因要做,否则在程序开始时调用srand()只需

;然后你可以多次调用rand()

。你可能每次都得到相同的结果

,因为你的程序运行时间不到一秒钟,所以每次从time()获得相同的结果




comp.lang.c常见问题解答位于< http://www.c-faq.com/>。问题13.15

到13.21处理随机数。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

"我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


Masud写道:


hi,

测试我希望在单个循环中在

不同范围之间生成不同的随机数。我尝试用以下方式解决

方式,但它总是描述相同的值。我正在寻找

这方面的建议。


#include< cstdlib>

#include< time.h> ;

#include< iostream.h>



其中一个标题是C ++标题而不是C标题。

C ++< cstdlibheader对应于C标题< stdlib.h>

其中一个标题是表面上的C ++标题,但不是,并且是

而不是C标题。

没有与C ++标题对应的C标题< iostream>。

注意C ++标题的名称。

其中一个标题是C标题,对于C ++不推荐使用:

C头< time.h有一个相应的C ++头< ctime>


这些暗示你想要发布到< news:comp.lang.c ++而不是

< news:comp.lang.c>。 C ++和C是不同的语言。当您向comp.lang.c ++发布

时,请设置标题

#include< cstdlib>

#include< ctime>

#include< iostream>

请他们支持我们没有为我们做的事情,检查早期的

帖子及其常见问题。您还需要学习为标准标识符正确指定

命名空间,无论是在每次使用时明确指定还是在使用时使用b $ b。条款,除非你想在庭外笑。


此外,在你的函数中随机调用srand()是一个逻辑错误。

调用srand( )之前使用rand()。 srand()的后续调用是

,不太可能做你想做的事。


不要使用''%''运算符来减少范围随机数。即使有一个好的伪随机数生成器,它也是一个严重的错误。如果

你已经完成了comp.lang.c的礼貌,即使在发布前一天跟随新闻组获取

,你也会知道这一点,以及

知道在哪里找到它是错误的原因以及如何应对它。但是,

当然,粗鲁是你作为程序员的宏伟表现。


>

int main()

{

int i,j;

for(i = 0; i< n; i ++)// N set rendom number

{

j = randomize(1,5); //第一个数字

cout<< j;

j = randomize(1,6); //第二个数字等等..

cout<< j;

}

}


int randomize(int LOW,int HIGH)

{

int value;

time_t seconds; //声明变量以保持秒数在

//时钟。

时间(&秒); //从系统时钟获取值并放入

//秒变量。

srand((unsigned int)seconds); //将秒转换为

//无符号整数。

value = rand() %(HIGH - LOW + 1)+ LOW; //获取随机

//介于LOW和HIGH之间的值

返回值;

}


On Thu,2007年8月2日02:03:44 -0700,Masud写道:


hi,

测试我希望在单个循环中在

不同范围之间生成不同的随机数。我尝试用以下方式解决

方式,但它总是描述相同的值。我正在寻找

这方面的建议。


#include< cstdlib>



这是一个C ++标题。在C中,它被称为< stdlib.h>


#include< time.h>

#include< iostream.h>



C中没有这样的标题。对于C ++,请转到comp.lang.c ++。

对于C,使用< stdio .h>

(无论如何,你的问题的答案在

语言中都是一样的,所以我会在这里回答)


int main()

{

int i,j;

for(i = 0; i< n; i ++)// N组重新随机数

{

j = randomize(1,5); //第一个数字

cout<< j;



在C中,使用printf("%d",j)


j = randomize(1 ,6); //第二个数字等等..

cout<< j;



当然你不想要任何空格?


}

}


int randomize(int LOW,int HIGH)



传统上,UPPERCASE名称用于预处理器宏。

编译器不在乎,但读者(包括你自己)可以

发现在

一瞥中确定某些东西是否有用是有帮助的。


{

int value;

time_t seconds; //声明变量以保持秒数
//时钟。

时间(&秒); //从系统时钟获取值并放入

//秒变量。

srand((unsigned int)seconds); //将秒转换为

//无符号整数。



< OTYou included< cstdlib> ;, not< stdlib.h> ;,所以你必须使用

std :: srand(或包括使用std :: srand;或使用命名空间std;

某处)。 < / OT>


value = rand()%(HIGH - LOW + 1)+ LOW; //在LOW之间获取随机的

//值和HIGH



www.c -faq.com ,问题13.15起。理论上,这是正确的
(前提是HIGH - LOW + 1远小于RAND_MAX,
否则它有偏差),但是有很多rand()的实现

非常差。


返回值;

}



-

Army1987(将NOSPAM替换为电子邮件)

永远不要归咎于可以通过愚蠢来充分解释的恶意。 - R. J. Hanlon(?)


hi,
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>
#include <time.h>
#include<iostream.h>

int main()
{
int i,j;
for(i=0;i<n;i++) //N set of rendom numbers
{
j=randomize(1,5); // first number
cout<<j;
j=randomize(1,6); // second number and so on..
cout<<j;
}
}

int randomize(int LOW,int HIGH)
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
return value;
}

解决方案

Masud <Go**********@gmail.comwrites:

for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>

Make this ''#include <stdlib.h>''.

#include <time.h>
#include<iostream.h>

Wrong language; that''s C++. Use ''#include <stdio.h>''.

int main()
{
int i,j;
for(i=0;i<n;i++) //N set of rendom numbers

There''s no declaration of ''n''. This obviously isn''t your actual code.

{
j=randomize(1,5); // first number

There''s been no declaration of ''randomize'' yet. You can probably get
away with this, but you should always declare functions before calling
them. In this case, you could just move the definition of randomize
above the definition of main.

cout<<j;
j=randomize(1,6); // second number and so on..
cout<<j;
}
}

int randomize(int LOW,int HIGH)
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
return value;
}

Unless you have a special reason to do otherwise, call srand() just
once at the beginning of your program; then you can call rand()
multiple times. You''re probably getting the same result each time
because your program takes less than a second to run, so you get the
same result from time() each time.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Questions 13.15
through 13.21 deal with random numbers.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Masud wrote:

hi,
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>
#include <time.h>
#include<iostream.h>

One of these headers is a C++ header and not a C header.
The C++ <cstdlibheader corresponds to the C header <stdlib.h>
One of these headers is superficially a C++ header, but isn''t, and is
not a C header.
There is no C header corresponding to the C++ header <iostream>.
Notice the name of the C++ header.
One of these headers is a C header, the use of which is deprecated for C++:
The C header <time.hhas a corresponding C++ header <ctime>

These suggest that you want to post to <news:comp.lang.c++and not to
<news:comp.lang.c>. C++ and C are different languages. When you do post
to comp.lang.c++, make your headers
#include <cstdlib>
#include <ctime>
#include <iostream>
Do them the favor, which you did not do for us, of checking earlier
posts and their FAQ. You also need to learn to properly specify the
namespace for standard identifiers, whether explicitly on each use or
which a "using" clause, unless you want to be laughed out of court.

Further, calling srand() in your function randomize is a logical error.
Call srand() once before using rand(). Subsequent calls of srand() are
very unlikely to do what you want.

Do not use the ''%'' operator to reduce the range of random numbers. It
is a gross error, even with a good pseudo-random number generator. If
you had done comp.lang.c the courtesy of following the newsgroup for
even one day before posting you would know this already, as well as
knowing where to find why it is an error and what to do about it. But,
sure, being rude is a sign of your grandeur as a programmer.

>
int main()
{
int i,j;
for(i=0;i<n;i++) //N set of rendom numbers
{
j=randomize(1,5); // first number
cout<<j;
j=randomize(1,6); // second number and so on..
cout<<j;
}
}

int randomize(int LOW,int HIGH)
{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.
value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH
return value;
}


On Thu, 02 Aug 2007 02:03:44 -0700, Masud wrote:

hi,
for a test i want to generate different random numbers between
different ranges in a single loop. I tried to solve in the following
way but it always profile same value. I am looking for suggestion in
this regard.

#include <cstdlib>

This is a C++ header. In C it is called <stdlib.h>

#include <time.h>
#include<iostream.h>

There is no such header in C. For C++, go to comp.lang.c++.
For C, use <stdio.h>
(anyway, the answer to your question is the same in both
languages, so I will answer here)

int main()
{
int i,j;
for(i=0;i<n;i++) //N set of rendom numbers
{
j=randomize(1,5); // first number
cout<<j;

In C, use printf("%d", j)

j=randomize(1,6); // second number and so on..
cout<<j;

Sure you don''t want any whitespace?

}
}

int randomize(int LOW,int HIGH)

Traditionally, UPPERCASE names are used for preprocessor macros.
The compiler doesn''t care, but the reader (including yourself) can
find it helpful to determine whether something is a macro at a
glance.

{
int value;
time_t seconds;// Declare variable to hold seconds on
//clock.
time(&seconds);//Get value from system clock and place in
//seconds variable.
srand((unsigned int) seconds);//Convert seconds to a
//unsigned integer.

<OTYou included <cstdlib>, not <stdlib.h>, so you must use
std::srand (or include a using std::srand; or using namespace std;
somewhere). </OT>

value = rand() % (HIGH - LOW + 1) + LOW;//Get the random
//value between LOW and HIGH

See www.c-faq.com, question 13.15 onwards. In theory, this is
correct (provided HIGH - LOW + 1 is much less than RAND_MAX,
otherwise it is biased), but with many implementations of rand()
it is very poor.

return value;
}

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)


这篇关于N组具有不同范围的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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