By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,122 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

Problem of random number generate..

P: n/a
Dear All:
I have a question about this below function.The purpose of this
function is to generate one number between a and b.
--------------------------
int gennum(int a, int b)
{
srand(time(NULL));
int range = b-a+1;
int x = (rand())%range;<-- problem occur here
int result = a+x;
return result;
}
----------------
this problem pass compile. However it has floating exception when
execution.
I don't know how to solve this problem. Please help me.
Thanks a lot!

Aug 9 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Sen-Lung Chen wrote:
I have a question about this below function.The purpose of this
function is to generate one number between a and b.
--------------------------
int gennum(int a, int b)
{
srand(time(NULL));
int range = b-a+1;
int x = (rand())%range;<-- problem occur here
int result = a+x;
return result;
}
----------------
this problem pass compile. However it has floating exception when
execution.
I don't know how to solve this problem. Please help me.
Thanks a lot!


I don't know how a floating point exception can occur when no floating
point type is present in the expression, so I can't help you with this
particular problem; however, the C FAQ has the suggested implementation
for generating random numbers from an interval. Your implementation is
not good since it uses %. Read the C FAQ at
http://www.eskimo.com/~scs/C-faq/top.html, the section 13 is the one of
interest for you.

V
Aug 9 '05 #2

P: n/a
Sen-Lung Chen wrote:
int range = b-a+1; this problem pass compile. However it has floating exception when
execution.
I don't know how to solve this problem. Please help me.
Thanks a lot!


I'd print out rnage. I got the strange feeling from the
behavior it is not a positive number.
Aug 9 '05 #3

P: n/a
Thanks a lot~~
I found the bug. I propagate 0 to range.
It's error.

Thanks for Ron and Victor.

Aug 9 '05 #4

P: n/a
Thanks a lot~~
I found the bug. I propagate 0 to range.
It's error.

Thanks for Ron and Victor.

Aug 9 '05 #5

P: n/a
Sen-Lung Chen wrote:
Dear All:
I have a question about this below function.The purpose of this
function is to generate one number between a and b.
--------------------------
int gennum(int a, int b)
{
srand(time(NULL));
int range = b-a+1;
int x = (rand())%range;<-- problem occur here
int result = a+x;
return result;
}
----------------
this problem pass compile. However it has floating exception when
execution.
I don't know how to solve this problem. Please help me.
Thanks a lot!


As others have pointed out:
(a) range might be negative.
(b) it might not be a good idea to use % for generating random
integers within a given interval since
(1) depending on RAND_MAX and range the numbers might not
be evenly distributed, and
(2) depending on the implementation of rand(), lower order
bits might not be random.

I would like to add that it is not a good idea to call srand()
within each call to gennum(). Depending on the frequence of calls,
you might get results that would not look random at all. E.g., on
my machine the program

#include <iostream>
#include <cstdlib>

using namespace std;

int gennum(int a, int b)
{
srand(time(NULL));
int range = b-a+1;
int x = (rand())%range;
int result = a+x;
return result;
}
int main ( void ) {

for ( unsigned int i=0; i<10; ++i ) {
std::cout << gennum(0,10) <<'\n';
}

}

produced:

news_group> a.out
6
6
6
6
6
6
6
6
6
6
Even if time(NULL) actually changes from call to call, you are giving
up all the theory that went into the design of rand(). Thus, you cannot
be sure that your results will pass any test for pseudo-randomness.
Best

Kai-Uwe Bux
Aug 9 '05 #6

P: n/a

"Sen-Lung Chen" <sl****@larc.ee.nthu.edu.tw> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Dear All:
I have a question about this below function.The purpose of this
function is to generate one number between a and b.
--------------------------
int gennum(int a, int b)
{
srand(time(NULL));
Should be:

srand((unsigned int)time(NULL));

Also note that 'srand()' should only be called once, near the start of
program execution. Don't call it before each call to 'rand()'.
int range = b-a+1;
int x = (rand())%range;<-- problem occur here
int result = a+x;
return result;
}
----------------
this problem pass compile. However it has floating exception when
execution.
I don't know how to solve this problem. Please help me.
Thanks a lot!


http://www.eskimo.com/~scs/C-faq/top.html
See sections 13.15 - 13.20
(This is the C FAQ, but this issue also applies to C++.)

Since your example code has no floating point operations, your
'floating exception' must be caused by something else in your
program. Try to create a small, compilable example program
that reproduces the problem and post that. (A benefit of this
approach is that often while doing this, you'll discover the
problem yourself.)

-Mike
Aug 9 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.