Topic: Defect report: inconsistencies in the definitions of rand() and


Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Sun, 12 Jan 2003 05:57:52 +0000 (UTC)
Raw View
James Kanze wrote:
> kuyper@wizard.net ("James Kuyper Jr.") wrote in message
> news:<3E1D6DD6.6030603@wizard.net>...
>
>>Pete Becker wrote:
>>
>>>Tom Hyer wrote:
>>
>> ....
>>
>>>>The aim is that users who do not care about the state can easily
>>>>ignore it.
>>>
>
>>>No. You must know about srand in order to use rand correctly.
>>
>
>>It's quite possible to accidentally use rand() correctly without that
>>knowledge. There are contexts where srand() is not needed, and a
>>programmer would not be penalized by his ignorance of srand() if he
>>happened to be writing code for one of those contexts.
>
>
> And how is the programmer supposed to know if he is writing code for one
> of those contexts or not, if he doesn't know of srand, and its
> interaction with rand?

He can't. That's not the point I was making. The point I was making is
that it's not necessary to know enough to guarantee that your code is
correct, in order to produce correct code.

> I agree with Pete on this one: rand and srand form a pair, and to use
> either rationally, you have to know both.

Possibly, depending upon what "use rationally" means.

However, by "use correctly", I meant only that the resulting code might
in fact be correct, not that the programmer knows enough to ensure its
correctness. Just as a random number generator can randomly produce the
correct value for 2+2, a programmer ignorant of srand() can successfully
produce correct code using rand().

Furthermore, I'd argue that the programmer is significantly more likely
to correctly use rand(), than the random number generator is to
correctly calculate 2+2 (unless that random number generator has an
extremely limited range). That's because there's a lot of situations in
which calling srand() isn't necessary, so a person who doesn't even know
that srand() exists can still have a decent chance of accidentally using
rand() correctly.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: petebecker@acm.org (Pete Becker)
Date: Thu, 9 Jan 2003 05:49:08 +0000 (UTC)
Raw View
Tom Hyer wrote:
>
> petebecker@acm.org (Pete Becker) wrote in message news:<3E1B2BF8.1B4DD7C7@acm.org>...
> > Tom Hyer wrote:
> > >
> > >     All these problems stem from the fact that the definition of
> > > rand() -- a function requiring no arguments -- is based on the
> > > pretense that random generation does not require state.
> >
> > That's just not true. srand initializes the random number generator's
> > state. It couldn't do that if rand didn't require state.
> >
>
> That's why I called it a pretense.  Let's go through this again.
>
> "the definition of rand() -- a function requiring no arguments"
>
> means that rand is defined as a function requiring no arguments.  In
> other words, the existence of the underlying state is not manifest.

True. It only appears in the documentation. How sneaky...

> The aim is that users who do not care about the state can easily
> ignore it.

No. You must know about srand in order to use rand correctly.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net ("James Kuyper Jr.")
Date: Thu, 9 Jan 2003 18:45:50 +0000 (UTC)
Raw View
Pete Becker wrote:
> Tom Hyer wrote:
....
>>The aim is that users who do not care about the state can easily
>>ignore it.
>
>
> No. You must know about srand in order to use rand correctly.

It's quite possible to accidentally use rand() correctly without that
knowledge. There are contexts where srand() is not needed, and a
programmer would not be penalized by his ignorance of srand() if he
happened to be writing code for one of those contexts.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: petebecker@acm.org (Pete Becker)
Date: Wed, 8 Jan 2003 00:29:58 +0000 (UTC)
Raw View
Tom Hyer wrote:
>
>     All these problems stem from the fact that the definition of
> rand() -- a function requiring no arguments -- is based on the
> pretense that random generation does not require state.

That's just not true. srand initializes the random number generator's
state. It couldn't do that if rand didn't require state.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]