You should not redefine RAND_MAX. It is a "read-only" quantity and
is provided for your information only.
The man page for rand() provided with Linux has the following
comment which you may find useful:
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1990 (1st ed, p. 207)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+((int) (1000000.0*rand()) % 10);
(which uses lower-order bits)."
--
Rouben Rostamian <rou...@math.umbc.edu>
>The source for a program I have just started writing is included below
>
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <time.h>
>
>#define RAND_MAX 32
Please read the FAQ before posting - it explains why this is wrong.
I quote:
(Note, by the way, that RAND_MAX is a *constant* telling you
what the fixed range of the C library rand() function is. You
cannot set RAND_MAX to some other value, and there is no way of
requesting that rand() return numbers in some other range.)
The FAQ also explains how to do what you want. Nearly all Usenet FAQs
can be ftp'd from rtfm.mit.edu under /pub/usenet.
--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------
:The source for a program I have just started writing is included below
:#include <stdio.h>
:#include <stdlib.h>
:#include <time.h>
:#define RAND_MAX 32
^^^^^^^^^^^^^^^^^^^^
Do't even think of doing this.
RAND_MAX is defined in stdlib.h and is a macro "which expands to an integral
constant expression, the value of which is the maximum value returned by the
`rand' function" (ISO 7.10).
For a complete an easy to read explanation of how to use rand(), see the FAQ
available from by anonymous FTP from rtfm.mit.edu. Steve Summit has done a
lot of work answering a large range of questions that new -- and sometimes
more advanced -- C programmers might have; take advantage of his labor. If
for some reason you cannot access the FAQ on line, pick up a copy in your
local bookstore, but make sure it is Summit's FAQ. In fact, it is normal
NETiquette - considered manditory by many - that you scan the FAQ for possible
answers to your question BEFORE posting to comp.lang.c.
:struct tm *systime;
:time_t t;
:int total;
:char yn;
:main()
:{
: t = time(NULL);
: systime = localtime(&t);
: rnd_total();
: printf("\n%d ", RAND_MAX);
: printf("\ntotal = %d", total);
: printf("\nseconds = %d", systime->tm_sec);
:}
:rnd_total()
:{
: srand (systime->tm_sec);
remember that more than one call to srand is wasteful and probably
self-defeating. A simpler and less costly way to call srand is
#include <stdlib.h>
#include <time.h>
#include <limits.h>
/* ... */
srand((unsigned)time(NULL) % UINT_MAX);
: total = rand();
:}
:When compiled (using DJGPP GCC) I get the screen output:
:32
:total = 40746
:seconds = 21
:where 32 is the RAND_MAX number I have defined, total = a random
:number between 0 and 65536, and seconds = the number of seconds that
:have passed in the current minute (used as seed for srand).
:After setting RAND_MAX to 32 surely total can only equal a number less
:than or equal to 32.
No; RAND_MAX tells you what the behavior or rand() is; it does not affect that
behavior. Again, get the FAQ, where all this is nicely explained.
:Can anyone help?
:Please e-mail me at
:Thanks - Greg
>After setting RAND_MAX to 32 surely total can only equal a number less
>than or equal to 32.
No.
RAND_MAX is #defined in stdlib.h to tell _you_ the maximim number that rand()
can generate - it does not control the range of numbers that rand() will
generate.
Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
Internet: ben...@triumf.ca | of one another only when one can be
Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html
or: http://vancouver-webpages.com/peter/index.html
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define RAND_MAX 32
struct tm *systime;
time_t t;
int total;
char yn;
main()
{
t = time(NULL);
systime = localtime(&t);
rnd_total();
printf("\n%d ", RAND_MAX);
printf("\ntotal = %d", total);
printf("\nseconds = %d", systime->tm_sec);
}
rnd_total()
{
srand (systime->tm_sec);
total = rand();
}
When compiled (using DJGPP GCC) I get the screen output:
32
total = 40746
seconds = 21
where 32 is the RAND_MAX number I have defined, total = a random
number between 0 and 65536, and seconds = the number of seconds that
have passed in the current minute (used as seed for srand).
After setting RAND_MAX to 32 surely total can only equal a number less
than or equal to 32.
Can anyone help?
> #include <stdlib.h>
> #define RAND_MAX 32
It is an error to re-#define the
same identifier unless the second token sequence is identical
to the first.
<snip>
> When compiled (using DJGPP GCC) I get the screen output:
Although, strictly speaking, your program should not have compiled,
the compiler at least must have warned you that RAND_MAX was redefined.
--
-----------------------------------------------------------------
Ed Breen
CSIRO, DMS Phone:+61 2 325 3208
Locked Bag 17, Nth Ryde Fax:+61 2 325 3200
NSW, Australia 2113 E-mail:e...@syd.dms.csiro.au
Building E6B URL:http://www.dms.csiro.au/~edb
Macquarie University Campus
------------------------------------------------------------------
> (...) After setting RAND_MAX to 32 surely total can only equal a
> number less than or equal to 32. Can anyone help?
You can't set RAND_MAX to make rand() do what you want, RAND_MAX is a
macro to tell you what rand() does. If you want random numbers in
another range, try shifting/dividing the return value from rand()
instead.
ciao,
TeSche
Hello all,
To perhaps make this a little clearer...
RAND_MAX should tell you what the maximum value rand() will
return is...
There is a general rule when getting random numbers:
RandomVal = (rand() % (Max - Base)) + Base;
Where 'Base' is the smallest random number you want,
and 'Max' is the largest random numbe you want + 1.
For example, to get a random number between 10 and 20, use
RandomVal = (rand() % (21 - 10)) + 10;
Remember you have to add 1 to 'Max' if you want to include the
max number, (ie. Had to use 21 to make 20 a legal result)...
I hope this helps some...
-Dave
:
:There is a general rule when getting random numbers:
: RandomVal = (rand() % (Max - Base)) + Base;
:Where 'Base' is the smallest random number you want,
:and 'Max' is the largest random numbe you want + 1.
Please retrieve the FAQ from rtfm.mit.edu before posting any more
bad "answers" to comp.lang.c. Someone might believe you, although I'm sure
you meant this as a joke. Next time include the smiley faces for those of us
who are humor-impaired.
:I hope this helps some...
Not likely.
-
Martin Ambuhl mam...@tiac.net
Honors Bridge Club, 115 E 57th, New York
/* mha - @ripco.com, @ix.netcom.com, etc. inactive
* all newsgroups follow-ups are also emailed */
I am back to apalogize for my previous post due to the fact
that it was not very accurate...The example I gave on how
to aquire a random numbers within a given range was poor in
reference to most RNGs. This is because the WHOLE value returned
by functions such as rand() are meant as random, but keeping a
subset of that number (ie. rand() % N always keeps the low bits)
turns out to be very non-random.
Well first, since the originator of this thread was trying
to redefine RAND_MAX, I figured he was new to C and possibly
to random numbers as a whole. My goal was to show in very clear
terms a concept for getting random numbers in a desired range, and
I believe the what I showed was a bit more clear than
(int)((double)rand() / ((double)RAND_MAX + 1) * N)
or
rand() / (RAND_MAX / N + 1)
Both of these being direct paste copies from the CFAQ which,
for the information of the nice man who decided to write me
personal nasty email, I did have a copy of.
Anyway, I guess a better approach would have been to show either
of these 2 above methods and explain why they worked, which I
should say, the CFAQ does not do. It simply says use them.
What they do essentially, if anyone is interested, is make a
a floating point value between 0 and 1 (rand() / RAND_MAX will
ALWAYS be between 0 and 1) then multiply your "Max" by that value,
resulting in a value thats always between 0 and "Max". Why this
is better is because it uses the WHOLE value returned by rand() ...
Anway, if you think what I'm posting is wrong, please post
corrections and don't just say "Dont ever try to redefine RAND_MAX"
or just say thats wrong and leave the thread.
YES there should be an explicit "Read the CFAQ" and I apologize again
for not mentioning the FAQ, my net-etiquitte is far from perfect. And
with the number of new programmers who read this group, you can spend
a lifetime posting nothing but "Read the FAQ you moron" messages
verywhere...
I'll leave that up to someone else.
-Dave
Which shows, once again, that you should never believe anything you
read in that book. The reason you shouldn't use anything resembling
the second expression is not because it uses lower-order bits but
because it doesn't use any bits -- it's just a very convoluted way to
write "j=1;". The comp.lang.c FAQ provides similar guidance with the
notable exception that it's actually correct.
-Larry Jones
When I want an editorial, I'll ASK for it! -- Calvin
>In article <56nkr6$1...@sun2.math.umbc.edu>, rou...@sun2.math.umbc.edu (Rouben
> Rostamian) writes:
>> In Numerical Recipes in C: The Art of Scientific Computing
>> (William H. Press, Brian P. Flannery, Saul A. Teukolsky,
>> William T. Vetterling; New York: Cambridge University
>> Press, 1990 (1st ed, p. 207)), the following comments are
>> made:
>> "If you want to generate a random integer between 1
>> and 10, you should always do it by
>>
>> j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
>>
>> and never by anything resembling
>>
>> j=1+((int) (1000000.0*rand()) % 10);
>>
>> (which uses lower-order bits)."
>
>Which shows, once again, that you should never believe anything you
>read in that book. The reason you shouldn't use anything resembling
>the second expression is not because it uses lower-order bits but
>because it doesn't use any bits -- it's just a very convoluted way to
>write "j=1;".
Not even that. (1000000.0*rand()) has a very good chance of not being in the
range of values that int can represent. So converting it to int has a very
good chance of resulting in undefined behaviour.