Problem of random number generate..

S

Sen-Lung Chen

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;
}
 
V

Victor Bazarov

Sen-Lung Chen said:
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
 
R

Ron Natalie

Sen-Lung Chen said:
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.
 
S

Sen-Lung Chen

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

Thanks for Ron and Victor.
 
S

Sen-Lung Chen

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

Thanks for Ron and Victor.
 
K

Kai-Uwe Bux

Sen-Lung Chen said:
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
 
M

Mike Wahler

Sen-Lung Chen said:
Dear All:
I have a question about this below function.The purpose of this
function is to generate one number between a and b.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top