Discussion:
[Mingw-users] RAND_MAX Problem (Why is it only 16-bit?)
Daniel John FitzGerald
2006-04-12 16:14:06 UTC
Permalink
Hello,

I am trying to find a POSIX-compatible IDE to migrate my research lab
away from Borland C++. We found Bloodshed's "Dev C++" with MinGW.
However, we've found a problem with RAND_MAX being set to only generate
up to a 16-bit number.

We tried redifining RAND_MAX to be 32-bit, and it compiled fine.
However, we still can't get any rand() values > 32k. Is there any way to
fix this problem?

I'm posting the question to this mailing list rather than the Dev C++
mailing list, because Dev C++ is using MinGW. However, could this be a
problem limited to Dev C++ or does it crop up in MinGW in general?
--
Dan FitzGerald
Computer Science, SUNY Geneseo

Theres no place like ~/

http://cs.geneseo.edu/~djf8
Brian Dessent
2006-04-12 16:44:01 UTC
Permalink
This post might be inappropriate. Click to display it.
Tor Lillqvist
2006-04-12 19:05:08 UTC
Permalink
In this case, MSVCRT.rand() only returns 15 bits of entropy and
changing a #define won't change that, it's just an aspect of the
simple pseudorandom algorithm they chose to use.
Yep, and it wasn't just Microsoft that did this. This was the normal
thing on Unix, too, in the timeframe when MSVCRT was specified and
written. (Hmm, and it probably still is because of backward
comptibility and historical reasons. lrand48() is the better RNG on
most Unix systems.)

--tml
Greg Chicares
2006-04-12 17:40:06 UTC
Permalink
Post by Daniel John FitzGerald
I am trying to find a POSIX-compatible IDE to migrate my research lab
away from Borland C++. We found Bloodshed's "Dev C++" with MinGW.
However, we've found a problem with RAND_MAX being set to only generate
up to a 16-bit number.
$grep -w RAND_MAX /Borland/BCC55/Include/*.h 2>/dev/null
/Borland/BCC55/Include/stdlib.h:#define RAND_MAX 0x7FFFU

C:/[0]$grep -w RAND_MAX /MinGW-20060119/include/*.h 2>/dev/null
/MinGW-20060119/include/stdlib.h: * RAND_MAX is the maximum value that may be returned by rand.
/MinGW-20060119/include/stdlib.h:#define RAND_MAX 0x7FFF

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_rand_max.asp
| RAND_MAX is defined as the value 0x7fff

ISO standard 9899, 7.20.2.1/5:
| The value of the RAND_MAX macro shall be at least 32767.

Looks like they all conform to standard C's requirements.
Post by Daniel John FitzGerald
We tried redifining RAND_MAX to be 32-bit, and it compiled fine.
A compiler is allowed to compile things like
#define NULL 1
#define stdout (FILE)(0)
#define RAND_MAX 100
without giving any diagnostic. But the program can fail.
It might crash. "Random" numbers might all be powers of
ten. Anything at all can happen. This is what the standard
calls "undefined behavior."
Post by Daniel John FitzGerald
However, we still can't get any rand() values > 32k. Is there any way to
fix this problem?
I'm posting the question to this mailing list rather than the Dev C++
mailing list, because Dev C++ is using MinGW. However, could this be a
problem limited to Dev C++ or does it crop up in MinGW in general?
http://mingw.org/mingwfaq.shtml#faq-what
| MinGW uses the Microsoft runtime libraries, distributed
| with the Windows operating system.

As long as it conforms to the standard, it's not broken.
Changing it would create a problem: that would break
compatibility with msvc. Someone might expect to set a
seed in srand() and get the same series of pseudorandom
numbers with both compilers, because they use the same
C runtime library. That may well be the reason why ms
didn't change this function when msw became a 32-bit
platform.

Probably it uses the POSIX 1003.1-2003 implementation
described here:
http://www.linuxmanpages.com/man3/rand.3.php
which is relatively poor. You can find source code on
the web for a better alternative like the BSD random()
function. If you're using C++, then use this library:
http://www.boost.org/libs/random/random-generators.html
which IIRC has been approved for addition to the C++
standard.
Earnie Boyd
2006-04-12 20:50:05 UTC
Permalink
Post by Daniel John FitzGerald
Hello,
I am trying to find a POSIX-compatible IDE to migrate my research lab
away from Borland C++. We found Bloodshed's "Dev C++" with MinGW.
You may be interested in googling for rhide+mingw.


Earnie Boyd

http://shop.siebunlimited.com
Daniel John FitzGerald
2006-04-14 15:58:00 UTC
Permalink
Everybody, thanks for all the help. I'm talking it over with my research
advisor and we're seeing what we can do about replacing our random
number generators.
Post by Daniel John FitzGerald
Hello,
I am trying to find a POSIX-compatible IDE to migrate my research lab
away from Borland C++. We found Bloodshed's "Dev C++" with MinGW.
However, we've found a problem with RAND_MAX being set to only generate
up to a 16-bit number.
We tried redifining RAND_MAX to be 32-bit, and it compiled fine.
However, we still can't get any rand() values > 32k. Is there any way to
fix this problem?
I'm posting the question to this mailing list rather than the Dev C++
mailing list, because Dev C++ is using MinGW. However, could this be a
problem limited to Dev C++ or does it crop up in MinGW in general?
--
Dan FitzGerald
Computer Science, SUNY Geneseo

Theres no place like ~/

http://cs.geneseo.edu/~djf8
Oscar Fuentes
2006-04-16 05:38:06 UTC
Permalink
Daniel John FitzGerald
Post by Daniel John FitzGerald
Hello,
I am trying to find a POSIX-compatible IDE
What is a POSIX-compatible IDE?
Post by Daniel John FitzGerald
to migrate my research lab
away from Borland C++. We found Bloodshed's "Dev C++" with MinGW.
However, we've found a problem with RAND_MAX being set to only generate
up to a 16-bit number.
rand is implemented on the C standard library. MinGW uses Microsoft's
C library (MSVCRT.DLL).

BTW, Borland's C library has the same value for RAND_MAX. (and not 16
bit wide, but 15: 0x7FFFU, exactly)
Post by Daniel John FitzGerald
We tried redifining RAND_MAX to be 32-bit, and it compiled fine.
This is not the solution. RAND_MAX is there to *inform* you what the
limit is. It is not a paramenter you can configure.
Post by Daniel John FitzGerald
However, we still can't get any rand() values > 32k. Is there any way to
fix this problem?
Implement your own random number generator. There are implementations
available on the web and on Comp Sci textbooks.
Post by Daniel John FitzGerald
I'm posting the question to this mailing list rather than the Dev C++
mailing list, because Dev C++ is using MinGW. However, could this be a
problem limited to Dev C++ or does it crop up in MinGW in general?
Dev C++ has nothing to do with this problem. MinGW/Microsoft does.
--
Oscar
Loading...