Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > rand and srand

Reply
Thread Tools

rand and srand

 
 
Bill Cunningham
Guest
Posts: n/a
 
      03-09-2008
I am stumped on this one. I tried two methods and something just doesn't
seem right. I'll try my new syle.

#include <stdio.h>
#include <stdlib.h>

main() {
srand(2000); /*seed unsigned */
printf("%u",rand());
}

Now I get a number much larger than 2000. Also when I also try RAND_MAX with
srand from time to time.

With main I was always told int was the default type and didn't need to be
declared. Main() has always worked. I hope this code is much more readable.

Bill


 
Reply With Quote
 
 
 
 
Falcon Kirtaran
Guest
Posts: n/a
 
      03-09-2008
Bill Cunningham wrote:
> I am stumped on this one. I tried two methods and something just doesn't
> seem right. I'll try my new syle.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> main() {
> srand(2000); /*seed unsigned */
> printf("%u",rand());
> }
>
> Now I get a number much larger than 2000. Also when I also try RAND_MAX with
> srand from time to time.
>
> With main I was always told int was the default type and didn't need to be
> declared. Main() has always worked. I hope this code is much more readable.
>
> Bill
>
>


The function rand() returns a value between 0 and RAND_MAX, not between
0 and what you pass to srand(). The latter function only sets the seed
for the random number generator (on which the next random number will be
based).

If you want to have a non-predictable sequence of random numbers, it's
often a good idea to set the seed to something that is not a literal and
will change each time the program is executed.

Additionally, if you want to generate a random number between 0 and
2000, the code to do that uses the modulus operator, thusly:

printf("%u\n", (rand() % 2001));

For one, without the \n (newline), nothing will separate your numbers
from each other. The second part of this is the % 2001. That divides
the value from rand() by 2001, and returns the remainder - so the number
can be anything from 0 to 2000. If rand() returns 2001, the modulus
makes it 0, etc.

As for your main(), I wasn't actually aware that such code was legal C.
My guess is that it is actually void main(), because you never return
a value from it, so the exit status of your program is most likely
undefined. It is a good idea, particularly in UNIX, to declare it int,
and return 0 at the end (unless the program failed).

--
--Falcon Kirtaran
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      03-09-2008
Bill Cunningham wrote:

> I am stumped on this one. I tried two methods and something just
> doesn't
> seem right. I'll try my new syle.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> main() {
> srand(2000); /*seed unsigned */
> printf("%u",rand());
> }
>
> Now I get a number much larger than 2000.


So what did you expect. Have you read the documentation for srand and
rand. The former "seeds" the pseudo-random number generator implemented
as rand, to start a new sequence of PRNs.

> Also when I also try
> RAND_MAX with srand from time to time.


That's not a very good idea. One common method is to use the return
value of the time function as the argument to srand.

> With main I was always told int was the default type and didn't need
> to be declared. Main() has always worked. I hope this code is much
> more readable.


Well, implicit int return has been removed from the current standard,
but you'll find that almost all compilers will still tolerate code that
uses this feature. However it's always a good programming practise to
specify the return type for all your functions. It makes for *more*
readable code, not less.

Also rand returns an int value. The correct specifier for printf is %d,
not %u.

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-09-2008

> The function rand() returns a value between 0 and RAND_MAX, not between
> 0 and what you pass to srand(). The latter function only sets the seed
> for the random number generator (on which the next random number will be
> based).
>
> If you want to have a non-predictable sequence of random numbers, it's
> often a good idea to set the seed to something that is not a literal and
> will change each time the program is executed.
>
> Additionally, if you want to generate a random number between 0 and 2000,
> the code to do that uses the modulus operator, thusly:
>
> printf("%u\n", (rand() % 2001));
>
> For one, without the \n (newline), nothing will separate your numbers from
> each other. The second part of this is the % 2001. That divides the
> value from rand() by 2001, and returns the remainder - so the number can
> be anything from 0 to 2000. If rand() returns 2001, the modulus makes it
> 0, etc.


Thanks so much for your advice. I haven't used code with modulus yet and
this is the first time I've seen it.

> As for your main(), I wasn't actually aware that such code was legal C. My
> guess is that it is actually void main(), because you never return a value
> from it, so the exit status of your program is most likely undefined. It
> is a good idea, particularly in UNIX, to declare it int, and return 0 at
> the end (unless the program failed).


The C99 standard wants and int. But all the compilers I've used accept
main() and main(int argc,char *argv[]) when parameters are used. Otherwise
automatic void of course with just () as parameters. Thanks for the advice
on rand. So is there really a need to use srand ( ) ?

Bill


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      03-09-2008
Bill Cunningham wrote:

<about main>

> The C99 standard wants and int.


Or a type compatible with int.

> [...] So is there really a need to use srand ( ) ?


Yes. If you want your next pseudo-random sequence to be different and
not repeat.

 
Reply With Quote
 
Falcon Kirtaran
Guest
Posts: n/a
 
      03-09-2008
Bill Cunningham wrote:
>> The function rand() returns a value between 0 and RAND_MAX, not between
>> 0 and what you pass to srand(). The latter function only sets the seed
>> for the random number generator (on which the next random number will be
>> based).
>>
>> If you want to have a non-predictable sequence of random numbers, it's
>> often a good idea to set the seed to something that is not a literal and
>> will change each time the program is executed.
>>
>> Additionally, if you want to generate a random number between 0 and 2000,
>> the code to do that uses the modulus operator, thusly:
>>
>> printf("%u\n", (rand() % 2001));
>>
>> For one, without the \n (newline), nothing will separate your numbers from
>> each other. The second part of this is the % 2001. That divides the
>> value from rand() by 2001, and returns the remainder - so the number can
>> be anything from 0 to 2000. If rand() returns 2001, the modulus makes it
>> 0, etc.

>
> Thanks so much for your advice. I haven't used code with modulus yet and
> this is the first time I've seen it.
>
>> As for your main(), I wasn't actually aware that such code was legal C. My
>> guess is that it is actually void main(), because you never return a value
>> from it, so the exit status of your program is most likely undefined. It
>> is a good idea, particularly in UNIX, to declare it int, and return 0 at
>> the end (unless the program failed).

>
> The C99 standard wants and int. But all the compilers I've used accept
> main() and main(int argc,char *argv[]) when parameters are used. Otherwise
> automatic void of course with just () as parameters. Thanks for the advice
> on rand. So is there really a need to use srand ( ) ?
>
> Bill
>
>


The function srand() sets the seed for the algorithm that returns the
next random number. It is highly recommended that you set it to
something or another, particularly (part of) the system time. If you
don't, the sequence will be static and not really that random at all.

--
--Falcon Kirtaran
 
Reply With Quote
 
Johannes Bauer
Guest
Posts: n/a
 
      03-09-2008
Falcon Kirtaran schrieb:

> Additionally, if you want to generate a random number between 0 and
> 2000, the code to do that uses the modulus operator, thusly:


No! It is not!

In the man mage it says

---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8----
Notes
The versions of rand() and srand() in the Linux C Library use the same
random number generator as random() and srandom(), so the lower-order
bits should be as random as the higher-order bits. However, on older
rand() implementations, and on current implementations on different
systems, the lower-order bits are much less random than the higher-order
bits. Do not use this function in applications intended to be portable
when good randomness is needed.

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, 1992 (2nd ed., p. 277)), the following
comments are made:

"If you want to generate a random integer between 1 and 10, you
should always do it by using high-order bits, as in

j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));

and never by anything resembling

j = 1 + (rand() % 10);

(which uses lower-order bits)."
----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----

Regards,
Johannes

--
"PS: Ein Realname wäre nett. Ich selbst nutze nur keinen, weil mich die
meisten hier bereits mit Namen kennen." -- Markus Gronotte aka Makus /
Kosst Amojan / maqqusz / Mr. G / Ferdinand Simpson / Quartillia
Rosenberg in dse <45608268$0$5719$>
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-09-2008
Falcon Kirtaran wrote, On 09/03/08 20:57:
> Bill Cunningham wrote:
>> I am stumped on this one. I tried two methods and something just
>> doesn't seem right. I'll try my new syle.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> main() {
>> srand(2000); /*seed unsigned */
>> printf("%u",rand());
>> }
>>
>> Now I get a number much larger than 2000. Also when I also try
>> RAND_MAX with srand from time to time.
>>
>> With main I was always told int was the default type and didn't need
>> to be declared. Main() has always worked. I hope this code is much
>> more readable.
>>
>> Bill
>>
>>

>
> The function rand() returns a value between 0 and RAND_MAX, not between
> 0 and what you pass to srand(). The latter function only sets the seed
> for the random number generator (on which the next random number will be
> based).
>
> If you want to have a non-predictable sequence of random numbers, it's
> often a good idea to set the seed to something that is not a literal and
> will change each time the program is executed.
>
> Additionally, if you want to generate a random number between 0 and
> 2000, the code to do that uses the modulus operator, thusly:
>
> printf("%u\n", (rand() % 2001));
>
> For one, without the \n (newline), nothing will separate your numbers
> from each other. The second part of this is the % 2001. That divides
> the value from rand() by 2001, and returns the remainder - so the number
> can be anything from 0 to 2000. If rand() returns 2001, the modulus
> makes it 0, etc.


There are issues with that method. See the comp.lang.c FAQ question
13.16 for why this is not good, and search the group for the long
debates there have been about whether the answer in the FAQ is correct.

> As for your main(), I wasn't actually aware that such code was legal C.


It was legal from when C was first created, continued to be legal when C
was standardised and was only banned by the latest C standard which is
not fully supported by most compilers.

> My guess is that it is actually void main(),


This is why reading a good book is generally far better than guessing.
Until C99 if the type was not specified it was implicitly assumed to be int.

> because you never return a
> value from it, so the exit status of your program is most likely
> undefined.


It is. Well, in C99 it became specified but the OP is not using C99 and
in any case being explicit is far better practice.

> It is a good idea, particularly in UNIX, to declare it int,
> and return 0 at the end (unless the program failed).


Agreed. The standard even provides two macros in stdlib.h, EXIT_SUCCESS
and EXIT_FAILURE with the obvious meanings, although 0 also means
successful termination. All other return values are non-portable.
--
Flash Gordon
 
Reply With Quote
 
Falcon Kirtaran
Guest
Posts: n/a
 
      03-09-2008
Johannes Bauer wrote:
> Falcon Kirtaran schrieb:
>
>> Additionally, if you want to generate a random number between 0 and
>> 2000, the code to do that uses the modulus operator, thusly:

>
> No! It is not!
>
> In the man mage it says
>
> ---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8---->8----
> Notes
> The versions of rand() and srand() in the Linux C Library use the same
> random number generator as random() and srandom(), so the lower-order
> bits should be as random as the higher-order bits. However, on older
> rand() implementations, and on current implementations on different
> systems, the lower-order bits are much less random than the higher-order
> bits. Do not use this function in applications intended to be portable
> when good randomness is needed.
>
> 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, 1992 (2nd ed., p. 277)), the following
> comments are made:
>
> "If you want to generate a random integer between 1 and 10, you
> should always do it by using high-order bits, as in
>
> j = 1 + (int) (10.0 * (rand() / (RAND_MAX + 1.0)));
>
> and never by anything resembling
>
> j = 1 + (rand() % 10);
>
> (which uses lower-order bits)."
> ----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----
>
> Regards,
> Johannes
>


You could do that too, I suppose. However, where lots of randomness is
not needed (a CGI application that returns random images), I've actually
just skipped a step and applied the modulus to the system time.

--
--Falcon Kirtaran
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      03-09-2008
On Sun, 09 Mar 2008 14:57:38 -0600, Falcon Kirtaran
<> wrote in comp.lang.c:

> Bill Cunningham wrote:
> > I am stumped on this one. I tried two methods and something just doesn't
> > seem right. I'll try my new syle.
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > main() {
> > srand(2000); /*seed unsigned */
> > printf("%u",rand());
> > }
> >
> > Now I get a number much larger than 2000. Also when I also try RAND_MAX with
> > srand from time to time.
> >
> > With main I was always told int was the default type and didn't need to be
> > declared. Main() has always worked. I hope this code is much more readable.
> >
> > Bill


[snip]

> As for your main(), I wasn't actually aware that such code was legal C.


Wherever did you get that idea? That code was perfectly legal in C up
until the 1999 update to the C language standard.

> My guess is that it is actually void main(), because you never return


Now you're getting silly. Prior to C99, everyplace where it was legal
to define or declare something without an explicit type, it was
implicitly typed as int. Never as void.

main()

....prior to C99, was exactly identical to:

int main()

....and is illegal under C99 and later versions, as all declarators
must explicitly declare a type.

> a value from it, so the exit status of your program is most likely
> undefined. It is a good idea, particularly in UNIX, to declare it int,
> and return 0 at the end (unless the program failed).


This is correct under any version of the C standard. If a program
"falls off the end" of main() without returning a value, the exit
status returned to the environment is undefined.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
srand versus srandom - srand with random() safe? Arijit Das C Programming 12 10-18-2011 08:08 AM
unique numbers using srand( ) and rand( ) functions in C++ August1 C++ 0 05-16-2004 08:01 AM
unique numbers using srand( ) and rand( ) functions in C++ August1 C++ 0 05-16-2004 08:01 AM
perl implementation of rand() and srand() Simon Perl Misc 7 03-02-2004 11:34 PM
unique numbers using srand( ) and rand( ) functions in C++ August1 C++ 4 12-08-2003 03:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57