Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

rand() function doesn't work well??

2 views
Skip to first unread message

cylin

unread,
Oct 22, 2004, 3:05:35 AM10/22/04
to
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;
}
-----------------------------------------


dandelion

unread,
Oct 22, 2004, 3:15:05 AM10/22/04
to

"cylin" <cy...@avant.com.tw> wrote in message
news:2trpliF...@uni-berlin.de...

> 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.

Sigurd Stenersen

unread,
Oct 22, 2004, 3:16:28 AM10/22/04
to
cylin wrote:
> 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"


--


Sigurd
http://utvikling.com


Larry Brasfield

unread,
Oct 22, 2004, 3:17:16 AM10/22/04
to
"cylin" <cy...@avant.com.tw> wrote in message news:2trpliF...@uni-berlin.de...
> 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]

--
--Larry Brasfield
email: donotspam_la...@hotmail.com
Above views may belong only to me.


Martin Ambuhl

unread,
Oct 22, 2004, 3:50:02 AM10/22/04
to
cylin wrote:
> 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;
}

Martin Ambuhl

unread,
Oct 22, 2004, 4:01:55 AM10/22/04
to
cylin wrote:

> 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.

cylin

unread,
Oct 22, 2004, 4:07:13 AM10/22/04
to

"Larry Brasfield" <donotspam_la...@hotmail.com> 撰寫於郵件新聞
:uF9yncAu...@tk2msftngp13.phx.gbl...

> "cylin" <cy...@avant.com.tw> wrote in message
news:2trpliF...@uni-berlin.de...
> > 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.
>

Yes, thanks.

Catalin Pitis

unread,
Oct 22, 2004, 4:28:32 AM10/22/04
to

"cylin" <cy...@avant.com.tw> wrote in message
news:2trt94F...@uni-berlin.de...

>
> "Larry Brasfield" <donotspam_la...@hotmail.com> 撰寫於郵件新聞
> :uF9yncAu...@tk2msftngp13.phx.gbl...
>> "cylin" <cy...@avant.com.tw> wrote in message
> news:2trpliF...@uni-berlin.de...
>> > 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.
>>
>
> Yes, thanks.
>

Use the random library from boost:

http://www.boost.org/libs/random/index.html

Catalin


Merrill & Michele

unread,
Oct 22, 2004, 8:34:53 AM10/22/04
to

> > cylin wrote:
> > Dear all,
> >
> > I try to use rand() to generate some integers;
> > but these integers are never larger than 40000.

> "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


Martin Ambuhl

unread,
Oct 22, 2004, 1:30:21 PM10/22/04
to
Merrill & Michele wrote:

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.

Ben Pfaff

unread,
Oct 22, 2004, 2:18:32 PM10/22/04
to
"Catalin Pitis" <catali...@iquestint.com.renameme> writes:

> Use the random library from boost:
>
> http://www.boost.org/libs/random/index.html

That's a C++ library as far as I can tell. Please don't post C++
suggestions to comp.lang.c, even cross-posted.
--
Ben Pfaff
email: b...@cs.stanford.edu
web: http://benpfaff.org

Merrill & Michele

unread,
Oct 22, 2004, 2:42:52 PM10/22/04
to
> >MPJ wrote:
> > 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.

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


Chris Schumacher

unread,
Oct 23, 2004, 5:38:34 PM10/23/04
to
"Larry Brasfield" <donotspam_la...@hotmail.com> wrote in
news:uF9yncAu...@tk2msftngp13.phx.gbl:

> 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==-

Larry Brasfield

unread,
Oct 23, 2004, 10:59:42 PM10/23/04
to
"Chris Schumacher" <ken...@hotmail.com> wrote in message news:Xns958BA94E5F82...@207.115.63.158...

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.

0 new messages