473,419 Members | 1,831 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,419 software developers and data experts.

rand()

Hi,

With VS .NET 2003 the rand() function sometimes returns a number equal to
RAND_MAX. The docs say: The rand function returns a pseudorandom integer in
the range 0 to RAND_MAX. Does this mean that 0 & RAND_MAX are included in
the range?

I am asking because I was using code from a book, which was developed on
Linux. The program occasionally dies, because the random number is used in
calculating an array index. It seems that the Linux version of rand()
returns a number less than RAND_MAX.

I am curious to know if this is a bug in the Windows or Linux version of
srand(), or they just like to be different. Or is the bug in the code from
the book?

The C# docs for Random.Next() are a little more clear:
A 32-bit signed integer greater than or equal to zero and less than
MaxValue.

Bill
--
http://www.componentsnotebook.com/
Nov 16 '05 #1
4 5749
Can't speak for gcc, but I know MSVC can return 0. I'm pretty sure most
compilers will generate a 0... MSDN for RAND_MAX says it is "the maximum
value that can be returned by the RAND function", so I assume it is
certainly meant to return RAND_MAX. Can't speak for gcc and the likes,
though... haven't used *nix in a few years ;)

hth,
-Dale

"Bill Burris" <wb*****@telusplanet.net> wrote in message
news:ut**************@tk2msftngp13.phx.gbl...
Hi,

With VS .NET 2003 the rand() function sometimes returns a number equal to
RAND_MAX. The docs say: The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Does this mean that 0 & RAND_MAX are included in
the range?

I am asking because I was using code from a book, which was developed on
Linux. The program occasionally dies, because the random number is used in calculating an array index. It seems that the Linux version of rand()
returns a number less than RAND_MAX.

I am curious to know if this is a bug in the Windows or Linux version of
srand(), or they just like to be different. Or is the bug in the code from the book?

The C# docs for Random.Next() are a little more clear:
A 32-bit signed integer greater than or equal to zero and less than
MaxValue.

Bill
--
http://www.componentsnotebook.com/

Nov 16 '05 #2
Marco de Boer wrote:

To get a random value between 0(inclusive) and x(exclusive),
you can use something like this:
#define RAND(x) ((size_t)((double)(x)*rand()/(1.0+RAND_MAX)))


That's OK if the number quality isn't essential, but it suffers from
non-uniformity. Doing it uniformly requires a loop that throws out
values in the partial range up to RAND_MAX.

--
Craig Powers
MVP - Visual C++
Nov 16 '05 #3
Looks like the problem is with the code in the book (AI Application
Programming).

In the 4th edition of Harbison & Steele it says: "Successive calls to rand
return integer values in the range 0 to the largest representable positive
value of type int (inclusive) ...".

The libraries that the author was using probably have RAND_MAX set to the
maximum for a 32-bit int, so the problem is much more rare then in VS which
has RAND_MAX set to the maximum for a 16-bit int. It looks like when
Microsoft made the transition from 16 to 32 bit, they never changed the
value of RAND_MAX. The Microsoft documentation for rand leaves out the word
(inclusive).

And none of this is a problem, since I now know what to fix in the rest of
the examples from the book. I am only using C because that is what is in
the book. After I run the C code from the book to see how it works, I
translate it to C#.

Bill

"Bill Burris" <wb*****@telusplanet.net> wrote in message
news:ut**************@tk2msftngp13.phx.gbl...
Hi,

With VS .NET 2003 the rand() function sometimes returns a number equal to
RAND_MAX. The docs say: The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Does this mean that 0 & RAND_MAX are included in
the range?

I am asking because I was using code from a book, which was developed on
Linux. The program occasionally dies, because the random number is used in calculating an array index. It seems that the Linux version of rand()
returns a number less than RAND_MAX.

I am curious to know if this is a bug in the Windows or Linux version of
srand(), or they just like to be different. Or is the bug in the code from the book?

The C# docs for Random.Next() are a little more clear:
A 32-bit signed integer greater than or equal to zero and less than
MaxValue.

Bill
--
http://www.componentsnotebook.com/

Nov 16 '05 #4
Craig Powers wrote:
That's OK if the number quality isn't essential, but it suffers from
non-uniformity. Doing it uniformly requires a loop that throws out
values in the partial range up to RAND_MAX.


I believe Marco's example is correct. The problem is with the simpler
(and more common)
x = (int) rand() % n;
--
Truth,
James Curran
www.noveltheory.com (personal)
www.njtheater.com (professional)
Nov 16 '05 #5

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

Similar topics

11
by: Tom McCallum | last post by:
Can any tell me why rand() is such a bad pseudo-random number generator. In all the articles I have read they say that you can predict the outcome of rand() but when I used its output with NIST's...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
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
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
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...
13
by: Spiros Bousbouras | last post by:
The standard says that rand() should return a pseudo-random number but what does pseudorandom mean ? If an implementation of rand() always returned the same number would it be conforming ? What if...
5
by: ds | last post by:
Hi all, rand() is not thread safe, a fact that may not be so bad after all.. However, I face the following problem: a piece of code uses rand() to get a random sequence, but always seeds with...
10
by: Rafael Cunha de Almeida | last post by:
Hi, I've found several sites on google telling me that I shouldn't use rand() % range+1 and I should, instead, use something like: lowest+int(range*rand()/(RAND_MAX + 1.0))
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,...
1
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...
0
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 project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.