Announcement

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

  • Random seed needed

    I am looking for a number or string that will be different in different runs of the same program. So far I can only think of using c(current_time) but that only changes once a minute. Is there a way to get the processid? That is what Stata uses for its temporary files but I don't see how to get it myself. The documentation for Stata random functions suggests saving the rng state, but that requires coordination across runs that I can't depend on. Any other suggestions?

  • #2
    If your computer can access the internet, this user-written program will download true random numbers and set them as seeds:

    Code:
    . ssc describe setrngseed
    
    -----------------------------------------------------------------------------------------------------
    package setrngseed from http://fmwww.bc.edu/repec/bocode/s
    -----------------------------------------------------------------------------------------------------
    
    TITLE
          'SETRNGSEED': module to to set random-number seed using truly random integer from random.org
    
    DESCRIPTION/AUTHOR(S)
          
          Typing setrngseed without arguments is an alternative to using
          Stata's  set seed command.  setrngseed obtains truly random
          integers from  www.random.org and uses those results to set the
          seed of Stata's  pseudo-random-number generator.
          
          KW: random numbers
          KW: seed
          
          Requires: Stata version 10
          
          Distribution-Date: 20101005
          
          Author: Antoine Terracol, Universite Paris I
          Support: email [email protected]
          
          Author: William Gould, StataCorp LP
          Support: email [email protected]
          
    
    INSTALLATION FILES                           (type net install setrngseed)
          setrngseed.ado
          setrngseed.sthlp
    -----------------------------------------------------------------------------------------------------
    David Radwin
    Senior Researcher, California Competes
    californiacompetes.org
    Pronouns: He/Him

    Comment


    • #3
      You could pass the current datetime as an argument to Stata and save the PID as a bash variable. In your do file:

      Code:
      args seed
      set seed `seed'
      And then you can call stata via:

      Code:
      $ stata -b do file.do `date +%s` & PID=`echo $!`

      Comment


      • #4
        Folks, have you found any alternative to the -setrngseed- ?

        I have received the following error

        Code:
        .  setrngseed
        (contacting http://www.random.org)
        file http://www.random.org/integers/?num=1&min=-1000000000&max=1000000000&col=1&base=10&format=plain&rnd=new not found
        server says file permanently redirected to https://www.random.org/integers/?num=1&min=-1000000000&max=1000000000&col=1&base=10&format=plain&rnd=new
        r(601);

        Comment


        • #5
          This may not get updated officially, but the issue can be resolved by a single find/replace of -http://- with -https://- as the protocols have moved to HTTPS since publication of the command.

          1) copy the setrngseed help file and ado to your PERSONAL ado path
          2) edit the setrngseed.ado to fix the URLs within
          3) uninstall the SSC version (-ado uninstall setrngseed-)
          4) verify Stata sees your edited version (-which setrngseed-)
          5) test it out (-setrngseed-)

          Comment


          • #6
            Leonardo, brilliant solution! Thank you so much. It helped A LOT!

            All the best,
            Tiago

            Comment


            • #7
              Getting a truly random seed from a outside source as described above is probably a better idea.

              But the current time of Stata is measured down to the second, so it changes not every minute, but rather every second.

              Code:
              . dis c(current_date)
              11 Aug 2021
              
              . dis c(current_time)
              10:42:26
              
              . dis c(current_date)
              11 Aug 2021
              
              . dis c(current_time)
              10:42:29
              
              . dis c(current_date)
              11 Aug 2021
              
              . dis c(current_time)
              10:42:31
              
              . dis c(current_date)
              11 Aug 2021
              
              . dis c(current_time)
              10:42:34
              current time changes every second.

              Comment


              • #8
                c(current_date) + c(current_time) etc may be combined with process and thread IDs?
                Code:
                java :
                    Macro.setLocal​("pid", Long.toString(ProcessHandle.current().pid()));  
                    Macro.setLocal​("tid", Long.toString(Thread.currentThread().getId()));  
                end

                Comment


                • #9
                  ref #8:
                  Code:
                  local end_java end
                  
                  qui forvalues i = 1/5 {
                      
                      java :
                  
                          String tms = Long.toString(System.currentTimeMillis());
                          String pid = Long.toString(ProcessHandle.current().pid());  
                          String tid = Long.toString(Thread.currentThread().getId());
                          Macro.setLocal​("id", tms + "_" + pid + "_" + tid);
                          
                      `end_java'
                  
                      noi macro dir _id
                  }
                  Code:
                  . qui forvalues i = 1/5 {
                  _id:            1628710362015_18100_948
                  _id:            1628710362139_18100_956
                  _id:            1628710362218_18100_964
                  _id:            1628710362298_18100_972
                  _id:            1628710362366_18100_980

                  Comment

                  Working...
                  X