473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem of random number generate..

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
6 1749
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
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
Thanks a lot~~
I found the bug. I propagate 0 to range.
It's error.

Thanks for Ron and Victor.

Aug 9 '05 #4
Thanks a lot~~
I found the bug. I propagate 0 to range.
It's error.

Thanks for Ron and Victor.

Aug 9 '05 #5
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: vikas | last post by:
hi, I want to generate random numbers in c++ (using vc++ 6.0) on hit of enter. I am using _kbhit(), but don't know how to reset it, so that I can use it again, can anybody help me with this. code...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
2
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
2
by: jerryau | last post by:
Hi, I'm creating a number generator program, that is supposed to generate 6 unique random numbers for each game. I want to generate this for 6 games. The problem is, that it works for the...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
10
by: Curt_C [MVP] | last post by:
If I use it in my page it's fine but when I put it in a Class file for calling it returns the same # for each call. Any ideas why? I'm sure it's something I'll slap myself for but the only samples...
7
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
2
by: ccarter45 | last post by:
I guess I really don't have a good understanding of methods, it's really confusing for me. I'm not asking anyone to solve this for me, but any help would be GREATLY appreciated. This is the...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.