473,405 Members | 2,141 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,405 software developers and data experts.

rand() function doesn't work well??

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;
}
-----------------------------------------
Jul 22 '05 #1
13 2999

"cylin" <cy***@avant.com.tw> wrote in message
news:2t*************@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.

Jul 22 '05 #2
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
Jul 22 '05 #3
"cylin" <cy***@avant.com.tw> wrote in message news:2t*************@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: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #4
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;
}
Jul 22 '05 #5
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.
Jul 22 '05 #6

"Larry Brasfield" <do***********************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D
:uF**************@tk2msftngp13.phx.gbl...
"cylin" <cy***@avant.com.tw> wrote in message

news:2t*************@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.
Here is my test code.

[cut because not at issue]

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

Jul 22 '05 #7

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

"Larry Brasfield" <do***********************@hotmail.com> ¼¶¼g©ó¶l¥ó·s»D
:uF**************@tk2msftngp13.phx.gbl...
"cylin" <cy***@avant.com.tw> wrote in message

news:2t*************@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
Jul 22 '05 #8
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
Jul 22 '05 #9
Merrill & Michele wrote:
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


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.
Jul 22 '05 #10
"Catalin Pitis" <ca***********@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: bl*@cs.stanford.edu
web: http://benpfaff.org
Jul 22 '05 #11
> >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
Jul 22 '05 #12
"Larry Brasfield" <do***********************@hotmail.com> wrote in
news:uF**************@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==-
Jul 22 '05 #13
"Chris Schumacher" <ke*****@hotmail.com> wrote in message news:Xn*****************************@207.115.63.15 8...
"Larry Brasfield" <do***********************@hotmail.com> wrote in
news:uF**************@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.


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.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
7
by: eyal.susser | last post by:
I hear rand() is not thread safe. I was using it, foolish man that I am. But what is meant exactly by unsafe? What can happen? Bizzare results from rand()? Something worse? Thanks, Eyal.
36
by: Ben Justice | last post by:
For a program in c, I need some random numbers for a system were people are placing bets. This is not a commerical project btw. Generally, I tend to rely on things from the standard library,...
36
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
15
by: cylin | last post by:
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,...
4
by: Siam | last post by:
Hi all, I'm writing a shell language in c++ that supports the generation of random numbers, and by its nature, each command must be executed in a new thread. The call to the random function from...
10
by: Fred | last post by:
Hi guys, I was wondering how could i get a random number exemple between 1 and 100 but with n% of getting some value over a number x. Thanks a lot in advance. Fred
8
by: remlostime | last post by:
i use g++ to generater rand number, now i find that the RAND_MAX is 32367 in my computer, how can i make a bigger rand number( the number is wihin in the integer(2^32-1))
15
by: Rich Fife | last post by:
Quick rand() question: I know you're not supposed to use "rand() % 1024" for instance, because it focuses on the lower bits. However, it seems to me that given that the argument is not a power...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.