Re: Rand() with base

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 05/21/04


Date: Fri, 21 May 2004 16:42:57 -0400 (EDT)


On Fri, 21 May 2004, Mabden wrote:
>
> "Joona I Palaste" <palaste@cc.helsinki.fi> wrote...
> >
> > Does the standard specify that the same seed has to always generate the
> > same sequence of PRNs? But it doesn't specify which seed has to generate
> > which sequence?

  Well, obviously, the first seed has to generate the first sequence,
and... :) Yes, 'srand' is supposed to actually seed the PRNG, and 'rand'
is supposed to produce repeatable results. No, the Standard does not
specify which sequences 'rand' must produce --- that would be tantamount
to specifying the 'rand' algorithm, which the Standard doesn't do (and we
all know this because we have all read the FAQ, right? ;)

> I am interested in this as well. I have a game that fills a bag with tiles
> (or it could be a deck of cards, whatever) and I would like to just store a
> (32-bit?) number that would re-generate the exact same sequence. It would
> limit game to have 4billion possible beginnings, but that might just be
> enough. ;-)

  The Standard only guarantees 15 bits for RAND_MAX. You want a 32-bit
seed (in conforming C code), you get your own PRNG. Other than that,
yes, 'srand' will help you.

-Arthur



Relevant Pages

  • Re: Cohens paper on byte order
    ... the external data model to a bit stream, ... comparable standards do specify external data ... sequence, and which therefore does not provide guidance ... standard character codes). ...
    (sci.crypt)
  • Re: Read only last line-
    ... I don't think it's required that rand() ever return RAND_MAX. ... The rand function computes a sequence of pseudo-random integers in ... and the Standard does not document that number. ... compare that to, say, the probability that the programmer who wrote ...
    (comp.lang.c)
  • Re: [C, C++] "(a=b) = c;" Illegal or Undefined?
    ... What part of the standard says that the second assignment happens after ... For better or worse, however, the standard disagrees. ... standard doesn't specify a sequence point that prevents it. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: printf output
    ... Undefined behavior as i is modified multiple times between ... sequence points. ... The C standard doesn't specify a well defined ...
    (comp.lang.c)
  • Re: printf output
    ... Undefined behavior as i is modified multiple times between sequence points. ... The C standard doesn't specify a well defined behavior for this. ...
    (comp.lang.c)