[Rd] WISH: set.seed(seed) to produce error if length(seed) != 1 (now silent)

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Fri Sep 17 15:41:49 CEST 2021


> I'd say a more serious problem would be using set.seed(.Random.seed) ...

Exactly, I'm pretty sure I also tried that at some point.  This leads
to another thing I wanted to get to, which is to add support for
exactly that case.  So, instead of having poke around with:

globalenv()$.Random.seed <- new_seed

where 'new_seed' is a valid ".Random.seed" seed, it would be
convenient to be able to do just set.seed(new_seed), which comes handy
in parallel processing.

/Henrik

On Fri, Sep 17, 2021 at 3:10 PM Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>
> I'd say a more serious problem would be using set.seed(.Random.seed),
> because the first entry codes for RNGkind, it hardly varies at all.  So
> this sequence could really mislead someone:
>
>  > set.seed(.Random.seed)
>  > sum(.Random.seed)
> [1] 24428993419
>
> # Use it to get a new .Random.seed value:
>  > runif(1)
> [1] 0.3842704
>
>  > sum(.Random.seed)
> [1] -13435151647
>
> # So let's make things really random, by using the new seed as a seed:
>  > set.seed(.Random.seed)
>  > sum(.Random.seed)
> [1] 24428993419
>
> # Back to the original!
>
> Duncan Murdoch
>
>
> On 17/09/2021 8:38 a.m., Henrik Bengtsson wrote:
> >> I’m curious, other than proper programming practice, why?
> >
> > Life's too short for troubleshooting silent mistakes - mine or others.
> >
> > While at it, searching the interwebs for use of set.seed(), gives
> > mistakes/misunderstandings like using set.seed(<double>), e.g.
> >
> >> set.seed(6.1); sum(.Random.seed)
> > [1] 73930104
> >> set.seed(6.2); sum(.Random.seed)
> > [1] 73930104
> >
> > which clearly is not what the user expected.  There are also a few
> > cases of set.seed(<character>), e.g.
> >
> >> set.seed("42"); sum(.Random.seed)
> > [1] -2119381568
> >> set.seed(42); sum(.Random.seed)
> > [1] -2119381568
> >
> > which works just because as.numeric("42") is used.
> >
> > /Henrik
> >
> > On Fri, Sep 17, 2021 at 12:55 PM GILLIBERT, Andre
> > <Andre.Gillibert using chu-rouen.fr> wrote:
> >>
> >> Hello,
> >>
> >> A vector with a length >= 2 to set.seed would probably be a bug. An error message will help the user to fix his R code. The bug may be accidental or due to bad understanding of the set.seed function. For instance, a user may think that the whole state of the PRNG can be passed to set.seed.
> >>
> >> The "if" instruction, emits a warning when the condition has length >= 2, because it is often a bug. I would expect a warning or error with set.seed().
> >>
> >> Validating inputs and emitting errors early is a good practice.
> >>
> >> Just my 2 cents.
> >>
> >> Sincerely.
> >> Andre GILLIBERT
> >>
> >> -----Message d'origine-----
> >> De : R-devel [mailto:r-devel-bounces using r-project.org] De la part de Avraham Adler
> >> Envoyé : vendredi 17 septembre 2021 12:07
> >> À : Henrik Bengtsson
> >> Cc : R-devel
> >> Objet : Re: [Rd] WISH: set.seed(seed) to produce error if length(seed) != 1 (now silent)
> >>
> >> Hi, Henrik.
> >>
> >> I’m curious, other than proper programming practice, why?
> >>
> >> Avi
> >>
> >> On Fri, Sep 17, 2021 at 11:48 AM Henrik Bengtsson <
> >> henrik.bengtsson using gmail.com> wrote:
> >>
> >>> Hi,
> >>>
> >>> according to help("set.seed"), argument 'seed' to set.seed() should be:
> >>>
> >>>    a single value, interpreted as an integer, or NULL (see ‘Details’).
> >>>
> >>>  From code inspection (src/main/RNG.c) and testing, it turns out that
> >>> if you pass a 'seed' with length greater than one, it silently uses
> >>> seed[1], e.g.
> >>>
> >>>> set.seed(1); sum(.Random.seed)
> >>> [1] 4070365163
> >>>> set.seed(1:3); sum(.Random.seed)
> >>> [1] 4070365163
> >>>> set.seed(1:100); sum(.Random.seed)
> >>> [1] 4070365163
> >>>
> >>> I'd like to suggest that set.seed() produces an error if length(seed)
> >>>> 1.  As a reference, for length(seed) == 0, we get:
> >>>
> >>>> set.seed(integer(0))
> >>> Error in set.seed(integer(0)) : supplied seed is not a valid integer
> >>>
> >>> /Henrik
> >>>
> >>> ______________________________________________
> >>> R-devel using r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>>
> >> --
> >> Sent from Gmail Mobile
> >>
> >>          [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-devel using r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-devel
> >>
> >
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>



More information about the R-devel mailing list