Hello,
Using Stata 14, I am simulating an r-class program and would like to save the rngstate of each replication of the simulation. Any help would be greatly appreciated!
Jessica
return scalar rng_state = c(rngstate)
clear all set more off set seed 123456 capture program drop test program define test, rclass syntax [, nobs(integer 10)] preserve set obs `nobs' return scalar rng_state = c(rngstate) g x = rnormal() sum x, mean return scalar x_avg = r(mean) restore end simulate , reps(3): test, nobs(10) type mismatch an error occurred when simulate executed test r(109);
------------------------------------------------------------------------- begin test --- - syntax [, nobs(integer 10)] - preserve - return scalar rng_state = c(rngstate) type mismatch --------------------------------------------------------------------------- end test ---
clear all set more off set seed 123456 capture program drop test program define test, rclass syntax [, nobs(integer 10)] preserve set obs `nobs' return scalar rng_state = "`c(rngstate)'" g x = rnormal() sum x, mean return scalar x_avg = r(mean) restore end simulate reps(3): test, nobs(10) type mismatch an error occurred when simulate executed test r(109);
------------------------------------------------------------------------- begin test --- - syntax [, nobs(integer 10)] - preserve - set obs `nobs' = set obs 10 - return scalar rng_state = "`c(rngstate)'" = return scalar rng_state = "XAA000000000001e240623a55849ed2854115472f70e95c2c42c05 >03a011992859d6e363499dc00deca553887850ad4deb4e0c5fc96dd3ff0d771756541b81a814b9913aeb3bad >e700ac49ffd6dc755a971838ce6630078571467f0e366002938e97b7608ad798218d43e064f4e9629087e06f >217dc19c30034e111de2ad2a4d533892c0eb121ac49802e9b07cac46a69eb147579c5bb073361ca98" type mismatch --------------------------------------------------------------------------- end test ---
clear all set more off macro drop _all scalar drop _all set seed 123456 capture program drop test program define test, rclass syntax [, nobs(integer 10)] preserve set obs `nobs' scalar rng = "`c(rngstate)'" return scalar rng = r(rng) g x = rnormal() sum x, mean return scalar x_avg = r(mean) restore end test, nobs(10)
. return list scalars: r(x_avg) = .2557781100273132 r(rng) = . . scalar list _all rng = XAA000000000001e240623a55849ed2854115472f70e95c2c42c0503a011992859d6e363499dc0 0deca553887850ad4deb4e0c5fc96dd3ff0d771756541b81a814b9913aeb3bade700ac49ffd6dc755a971838ce6 630078571467f0e366002938e97b7608ad798218d43e064f4e9629087e06f217dc19c30034e111de2ad2a4d5338 92c0eb121ac49802e9b07cac46a69eb147579c5bb073361ca98fa09263627207e7f0bdac04e3e3b1c09fb100e61 b647834b306
clear all set more off macro drop _all scalar drop _all set seed 123456 capture program drop test program define test, rclass syntax [, nobs(integer 10)] preserve set obs `nobs' return local rng_state "`c(rngstate)'" scalar rng = "`c(rngstate)'" return scalar rng = r(rng) g x = rnormal() sum x, mean return scalar x_avg = r(mean) restore end test, nobs(10) return list scalar list _all simulate, reps(3): test, nobs(10) list
. return list scalars: r(x_avg) = .2557781100273132 r(rng) = . macros: r(rng_state) : "XAA000000000001e240623a55849ed2854115472f70e95c2c42c0503a011992859d6e363499dc00deca553887850ad4deb4e0c5fc96dd3ff0d771756541b81a814b9913aeb3bade.." . scalar list _all rng = XAA000000000001e240623a55849ed2854115472f70e95c2c42c0503a011992859d6e363499dc00deca553887850ad4deb4e0c5fc96dd3ff0d771756541b81a814b9913aeb3bade700ac49ffd6dc755a971838ce6630078571467f0e366002938e97b7608ad798218d43e064f4e9629087e06f217dc19c30034e111de2ad2a4d533892c0eb121ac49802e9b07cac46a69eb147579c5bb073361ca98fa09263627207e7f0bdac04e3e3b1c09fb100e61b647834b306c2bed439173dd... . simulate, reps(3): test, nobs(10) command: test, nobs(10) x_avg: r(x_avg) rng: r(rng) Simulations (3) ----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5 .xx . list +-----------------+ | x_avg rng | |-----------------| 1. | .2557781 . | 2. | -.2267316 . | 3. | .1837081 . | +-----------------+
return scalar rng = r(rng)
clear all set more off macro drop _all scalar drop _all set seed 123456 capture program drop test program define test, rclass syntax [, nobs(integer 10)] preserve set obs `nobs' return local rng `c(rngstate)' g x = rnormal() sum x, mean return scalar x_avg = r(mean) restore end drop _all tempfile results save `results', emptyok local nreps 3 forvalues i = 1/`nreps' { test return list local rng `r(rng)' local x_avg `r(x_avg)' drop _all set obs 1 gen x_avg = `x_avg' gen strL rng = `"`rng'"' append using `results' save `"`results'"', replace } use `results', clear
observation number out of range Observation number must be between 11 and 281,474,976,710,654. (Observation numbers are typed without commas.) r(198);
return local state = c(rngstate)
return scalar state = c(rngstate)
program return_state, rclass set seed 12345 local l = c(rngstate) scalar s = c(rngstate) return local statel = "`l'" return local states = s end return_state return list
clear all set more off macro drop _all scalar drop _all set seed 123456 capture program drop test program define test, rclass syntax [, nobs(integer 10)] preserve set obs `nobs' scalar state1 = substr(c(rngstate), 1, 2000) scalar state2 = substr(c(rngstate), 2001, 4000) scalar state3 = substr(c(rngstate), 4001, .) g x = rnormal() sum x, mean return scalar x_avg = r(mean) restore post buffer (r(mean)) (state1) (state2) (state3) end postfile buffer double x_avg str2000(state1 state2 state3) using results, replace simulate, reps(3): test, nobs(10) postclose buffer use results, clear gen strL state = state1+state2+state3 drop state1 state2 state3
Comment