Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Setting Seed in Stata to Current Date and Current Time

    Hello,

    I would like to set seed to the current date and, alternatively, the current time. Unfortunately, my attempts using the following code haven't been successful, because the set seed command requires an integer:

    scalar time = now()
    set seed time

    scalar date = today()
    set seed date

    I appreciate your help.

    Thank you.

  • #2
    You want to evaluate the scalar. Type

    Code:
    set seed `=today()'
    now() is probably too large to be a seed.

    Whether the date on which you draw pseudo-random numbers is a good seed is debatable.

    Comment


    • #3
      #2 Much of the point of setting a seed is ensuring reproducibility for those who want it. Even setting it to the current daily date (forget about millisecond) is of no use in this respect unless those publishing results tell you when they ran their code.

      Comment


      • #4
        All random number generators require a seed. When you don't set the seed, one is generated for you from a "random" (read arbitrary) source of data: Often it is the date and time, or something equivalent, like the number of milliseconds since the unix epoch. So I wonder what OP hopes to gain from setting the seed like this that they aren't already getting from the built in random number generator.

        Comment


        • #5
          Thank you for your messages.

          To clarify, I am NOT setting a seed for its conventional usage (e.g., replicability, drawing pseudo-random numbers, etc.). I am aware that now() is too large of a seed, as the help file indicates "# is any number between 0 and 2^31-1 (or 2,147,483,647).” However, today() is not too large, and the current time in the format of hhmmss is also within the acceptable range. When set seed is not specified by the user, and a command like runiform() is used, Stata employs its own built-in seed, "which is set to 123456789 every time Stata is launched. This means that runiform() produces the same sequence each time you start Stata" as stated in the help file. Consequently, a "random" seed generated by Stata is not truly random, and this is the core of my issue.

          I wish to set seed to the current date or time for a personal project unrelated to reproducibility or academic research. I developed a program that selects a random value from a local of values, and my intention is for each user using the program to draw a distinct value. Therefore, utilizing the current time ensures that, in practice, users can draw different values when using the command (approximately) simultaneously. If this isn't feasible, setting the seed to the current date would enable users to draw a different value each day of the year. The specific reasons for preferring a seed dependent on date or time are not crucial. My inquiry is simply whether this is possible.

          Comment


          • #6
            Originally posted by Max Williams View Post
            I wish to set seed to the current date or time . . . my intention is for each user using the program to draw a distinct value. Therefore, utilizing the current time ensures that, in practice, users can draw different values when using the command (approximately) simultaneously. . . . My inquiry is simply whether this is possible.
            Yes.

            A quick-and-dirty implementation would look something like the following.
            Code:
            set seed `: display %13.0f mod(now(), 2^31-1)'
            If you want to avoid sequential seeds then you can first convert the integer to a string and reverse it.
            Code:
            local now : display %15.0f now()
            local won = strreverse("`now'")
            set seed `: display %13.0f mod(`won', 2^31-1)'

            Comment


            • #7
              Thank you, Joseph. The code functions exceptionally well.

              Comment


              • #8
                Just to say that I think everyone is right here. Without more information, this sounds an odd request that experienced people might well query, but your reasons are compelling, and many thanks for explaining them in detail.

                Comment

                Working...
                X