Announcement

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

  • set seed yileds different results across OS/Stata Version

    I am attempting to obtain consistent results with a Monte Carlo simulation across two computers and noticed that the
    Code:
    set seed
    command doesn't generate the same random numbers across the two. Here's the relevant info:

    MacOS Catalina v 10.15.6
    Stata 12.1


    Code:
    set seed 1975
    set obs 5
    
    gen x=runiform()
    list x
    consistently yields the following values on a new instance of Stata:

    .5811128
    .9205765
    .4357026
    .3658487
    .379546


    Windows 10 Home
    Stata 16.1


    the same commands consistently yield the following values on a new instance of Stata:

    .8699474
    .5506942
    .4135425
    .0386538
    .8706713

    Does anyone know how can I obtain consistent results across these two computers with different operating systems and Stata versions?

  • #2
    Also, if anyone knows how to edit my post title, I misspelled "yields"

    Comment


    • #3
      In your copy of Stata 16, read the output of
      Code:
      help set rng
      In Stata 14 the random number generator was improved, at the cost of needing to specify the former random number generator when you are trying to maintain compatibility with earlier versions of Stata.

      Comment


      • #4
        Thank you William, that helped! From reading up on the new rng, I saw that I could set the version of Stata on the Windows machine to 12.1 so that a seed of 1975 meant the same thing as it does on the Mac. The random numbers are now the same!

        However, there is one lingering issue it seems. Before one random number generation, my code has a merge command where I combine 2 data sets. The random numbers generated after the merge are the same now across computers, but the data post-merge on the Windows machine is not in the same order of that on the Mac! So although the random numbers are the same across computers in each row, the rows now have different observations in them even though the code is identical.

        Is there a way to hold constant the sort after merge without sorting the data ourselves?
        Last edited by Craig Kerr; 15 Nov 2020, 23:35.

        Comment


        • #5
          It there are ties in your sorting variable, the sorting for these ties is randomized. To get the same sort order you can specify the stable option of sort.

          Edit: Sorry, I see now your question is about merge, not about sort. I don't think merge has any options to control the sort order I'm afraid, but have you checked whether the data is in the same sort order before merge? This might solve the problem.
          Last edited by Wouter Wakker; 16 Nov 2020, 02:27.

          Comment


          • #6
            From the full documentation for merge in the Stata Data Management Reference Manual PDF included in the Stata installation and accessible from Stata's Help menu.

            The new observations are similarly ordered so that they all appear at the end of what was the master. If the master originally had N observations, then the new observations, if any, are the (N+1)st, (N+2)nd, and so on. ...

            The merged result is unsorted except for a 1:1 merge, where there are only matched observations. Here the dataset is sorted by the key variables.
            As I read this, it seems there is no "sort" following the merge to hold constant; the data is ordered by the workings of the merge command, which may change from version to version of Stata.

            Comment


            • #7
              Wouter: yes everything was identical before the merge across versions.

              William: That does seem to be the case. Since I want to randomly select data after the merge, I guess there's no harm in manually sorting the data before the randomization to ensure that the order of observations is the same across computers (versions).

              Comment

              Working...
              X