rand() function doesn't work well??

C

cylin

Dear all,

I try to use rand() to generate some integers;
but these integers are never larger than 40000.

I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What's wrong?

Here is my test code.
-------------------------------------------
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
srand(time(NULL));
int count=0;
for(;;) {
int value=rand();
if (value>40000) {
cout << value << endl;
if (count++ >10) {
break;
}
}
}
return 0;
}
-----------------------------------------
 
D

dandelion

cylin said:
Dear all,

I try to use rand() to generate some integers;
but these integers are never larger than 40000.

I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What's wrong?

Knuth, the art of computer programming, Vol III and a mathematical analysis
of the sources (if provided) will probably tell you.
 
S

Sigurd Stenersen

cylin said:
Dear all,

I try to use rand() to generate some integers;
but these integers are never larger than 40000.

I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What's wrong?

RTFM

"The rand function returns a pseudorandom integer in the range 0 to
RAND_MAX"
 
L

Larry Brasfield

cylin said:
Dear all,

I try to use rand() to generate some integers;
but these integers are never larger than 40000.

I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What's wrong?

What's wrong is your expectation that RAND_MAX
will be greater than 32767. You mistake a result that
happened to meet your incorrect expectation for
confirmation that your expectation is good. The fact
is that an implementation with RAND_MAX no
greater than 32767 can still conform (perfectly) to
the standard requirements placed on the C library.

If you truly need a larger range of random integers,
either pick a compiler and runtime libary that does
what you want, or find a pseudo-random number
generator that does. Many are floating around.
Here is my test code.
[cut because not at issue]
 
M

Martin Ambuhl

cylin said:
Dear all,

I try to use rand() to generate some integers;
but these integers are never larger than 40000.

I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What's wrong?

Here is my test code.

Please don't post C++ code to comp.lang.c unless you are *sure* that it
is also C, and that the semantics are the same. These are two different
languages. In your case, the <cstdlib>, <iostream>, and <ctime> headers
are not C; the "using" statement is not C; the use of declarations other
than either at the top of a block or outside of all functions is now C,
but only for the C99 standard; the use of undeclared variables cout and
endl, subjected to strange use of the shift left operator is not C.

Your test condition 'if (value>40000)' is backwards if you mean to never
output a value greater than 40000. If one compiler produced code
resulting in output you expected, and the other did not, then you did
not give them the same input code.

Since you posted to comp.lang.c, you are entitled to a C version that
does what you seem to want:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
int value;
int count;
srand((unsigned) time(0));
for (count = 0, value = rand(); count < 10; value = rand()) {
if (value > 40000)
continue;
printf("%d\n", value);
count++;
}
return 0;
}
 
M

Martin Ambuhl

cylin said:
Dear all,

I try to use rand() to generate some integers;
but these integers are never larger than 40000.

I see that I misunderstood your problem. I imagined that the "never
larger than 40000" was part of the specification, rather than the
problem. rand() returns an int in the range 0 to RAND_MAX, and in C
RAND_MAX must be at least 32767. For implementations with
RAND_MAX==32767 (or any other value not greater than 40000), you will
obviously not see a result greater than 40000. This is all covered in
your documentation. It is provided for a reason. If you have somehow
gotten a compiler without its documentation, buy a basic textbook.
 
C

cylin

Larry Brasfield said:
What's wrong is your expectation that RAND_MAX
will be greater than 32767. You mistake a result that
happened to meet your incorrect expectation for
confirmation that your expectation is good. The fact
is that an implementation with RAND_MAX no
greater than 32767 can still conform (perfectly) to
the standard requirements placed on the C library.

If you truly need a larger range of random integers,
either pick a compiler and runtime libary that does
what you want, or find a pseudo-random number
generator that does. Many are floating around.

Yes, thanks.
Here is my test code.
[cut because not at issue]
 
M

Merrill & Michele

"Martin Ambuhl wrote:
I see that I misunderstood your problem. I imagined that the "never
larger than 40000" was part of the specification, rather than the
problem. rand() returns an int in the range 0 to RAND_MAX, and in C
RAND_MAX must be at least 32767. For implementations with
RAND_MAX==32767 (or any other value not greater than 40000), you will
obviously not see a result greater than 40000. This is all covered in
your documentation. It is provided for a reason. If you have somehow
gotten a compiler without its documentation, buy a basic textbook.

This is a record-setting event in terms of politeness and helpfulness given
your crossposting use of that other language. MPJ
 
M

Martin Ambuhl

Merrill said:
This is a record-setting event in terms of politeness and helpfulness given
your crossposting use of that other language. MPJ

Note that it was not "my" crossposting. The OP crossposted. Since his
question proves that he had not checked the newsgroups' FAQs and he is
not a known comp.lang.c poster, it would have been presumptuous for me
to have done other than to leave his crossposting unchanged.
 
M

Merrill & Michele

MPJ said:
Note that it was not "my" crossposting. The OP crossposted. Since his
question proves that he had not checked the newsgroups' FAQs and he is
not a known comp.lang.c poster, it would have been presumptuous for me
to have done other than to leave his crossposting unchanged.

'Your' did refer to the OP and was poor English. MPJ
 
C

Chris Schumacher

If you truly need a larger range of random integers,
either pick a compiler and runtime libary that does
what you want, or find a pseudo-random number
generator that does. Many are floating around.

Of course, on the other hand you could simply use the following code;


srand( time(0) );
b = ( rand() * ( ( rand() % 10 ) + 1 ) );

I don't know if that'll give the original poster the range of numbers he
needs, but it should help.



-==Kensu==-
 
L

Larry Brasfield

Chris Schumacher said:
Of course, on the other hand you could simply use the following code;


srand( time(0) );
b = ( rand() * ( ( rand() % 10 ) + 1 ) );

I don't know if that'll give the original poster the range of numbers he
needs, but it should help.

He may be after more than just a greater range.
For example, he might have hoped for values
within the range to have approximately equal
probabilities of being produced. If that common
requirement existed, he would be disappointed
in your solution. I would point out why that is so,
but I think it would be better for you (or anybody
else considering rolling their own PRNG on the
cheap) to consider the problem on their own. If
you can easily see the problem, then consider
how readily a more subtle defect could occur.
If you cannot see the problem, then I would
encourage you to rely on other people's well-
tested work in this realm.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top