Announcement

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

  • Random seed resumes and can't change the random number generator using do file.

    Hi Statalist,

    I found that every time when I open Stata, the random seed resumes to a status. For example, every time when I open Stata and draw 10 values from the uniform distribution, Stata always gives me the same results. However, R and MS-Excel do not behave like this -- they will give me different values when I re-open them and draw the values. Therefore I would like to ask whether Stata is designed to behave like that, or whether there is something wrong in my system.

    Code:
    forvalue i=1/10 {
     di runiform()
    }
    
    .3488717
    .26688571
    .13664629
    .02855687
    .86893327
    .3508549
    .07110509
    .32336795
    .55510316
    .87599101
    Moreover, I found that I cannot change the random number generator from mt64 to kiss32 or the other around by using the do file. I can only change the rng by running the syntax from the command windows. Again, I would like to know whether that is by design or whether that means something wrong in my system.

    Thank you very much.

    Best regards,
    Kirin

  • #2
    From the set seed help file

    The sequences the random-number functions produce are determined by the seed, which is just a number and which is set
    to 123456789 every time Stata is launched. This means that runiform() produces the same sequence each time you start
    Stata. The first time you use runiform() after Stata is launched, runiform() returns 0.348871704556195. The second
    time you use it, runiform() returns 0.266885709753138. The third time you use it, ....
    I have no problem setting the RNG within a do-file:
    Code:
    . do "C:\Users\SCOTT~1.MER\AppData\Local\Temp\1\STD00000000.tmp"
    
    . set rng  kiss32
    
    . disp c(rng_current)
    kiss32
    
    . disp c(rngstate)
    X075bcd151f123bb5159a55e50022865700053e56
    
    . forvalue i=1/2 {
      2.  di runiform()
      3. }
    .13698408
    .64322067
    
    . disp c(rng_current)
    kiss32
    . 
    . set rng mt64
    
    . disp c(rng_current)
    mt64
    
    . disp c(rngstate)
    XAA000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000001391839
    
    . forvalue i=1/2 {
      2.  di runiform()
      3. }
    .3488717
    .26688571
    
    . disp c(rng_current)
    mt64
    
    . 
    . 
    end of do-file
    
    .

    Comment


    • #3
      Originally posted by Kirin_Guess View Post
      Hi Statalist,

      ..., every time when I open Stata and draw 10 values from the uniform distribution, Stata always gives me the same results. ...
      This is in part to achieve reproducibility of results, in part due to the way randomness is implemented: pseudorandomness.
      A while ago I wrote truernd specifically for the purposes of using the web service that provides a bit more "random" numbers than those Stata is producing. Particularly useful was getting a random number and initializing the sequence with it.

      It stopped working in Stata 13 and 14, a confirmed issue by the Stata developers:
      http://www.statalist.org/forums/foru...ps-in-stata-13

      Every program you quote will have to deal with the issue one way or the other. A common pattern would be to initialize based on the clock (e.g. get the milliseconds). This can easily be replicated in Stata.

      Other types of generators may be globally unique, and not requiring initialization. You can get a GUID from Windows e.g. via a plugin.

      Explain, why do you need the sequence to be different?

      Best, Sergiy Radyakin

      Comment


      • #4
        Scott Merryman, thanks for pointing out that quote. I should have read the manual more carefully.
        Sergiy Radyakin, thanks for your explanation and advice. I just wonder whether it would be better to prevent using the same random seed every time Stata launches, but I don't really know its pros and cons. By the way, does any one know how to use Stata to obtain the current time in milliseconds? "di c(current_time)" only returns the time in second.

        Best regards,
        Kirin

        Comment

        Working...
        X