473,743 Members | 2,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3033

"cylin" <cy***@avant.co m.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.co m.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************* **********@hotm ail.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************ ***********@hot mail.com> 撰寫於郵件新聞
:uF************ **@tk2msftngp13 .phx.gbl...
"cylin" <cy***@avant.co m.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************* **********@hotm ail.com
Above views may belong only to me.

Jul 22 '05 #7

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

"Larry Brasfield" <do************ ***********@hot mail.com> 撰寫於郵件新聞
:uF************ **@tk2msftngp13 .phx.gbl...
"cylin" <cy***@avant.co m.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==327 67 (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

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

Similar topics

4
10759
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 consisting of 6 numbers - with a total of 50 groups of numbers. A well-known girl that some publishing companies use to provide introductory level textbooks to various Junior Colleges in the U.S., not surprisngly, asks for this same exact...
7
7366
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
7131
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, because they're written by people with skills far above mine. Hence, I've always used rand() and FALSELY assumed it could produce unpredictable random numbers and could be used in many situations even if security was an issue. Firstly, lets assume...
36
2688
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
518
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, the result is perfect. What's wrong? Here is my test code.
4
2796
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 my language simply propogates up to the rand( ) function from c++. For some reason, C++ will give each thread independent use of their own rand( ) function, so that a rand( ) call from one thread won't affect another thread's call to rand( )....
10
2098
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
4729
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
3938
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 of two (or near a power of two), that this is not an issue. The upper bits will participate equally in the result with the lower. Am I missing something? Thanks!
0
8970
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9486
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9277
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9214
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8218
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3286
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2195
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.