Parallel Random Number Generation

R comes with built-in parallel random number generation facilities. It’s quite easy to do PRNG correctly with only a default installation of R. Without getting into the details, which are far outside the scope of this documentation, the goal is to use an appropriate random number generator and an appropriate seed such that you get a new stream that can be treated as independent of other streams.

The function prngInit does all the setup for you, so that you can generate draws from distributions on multiple processors simultaneously. All you need to do is pass it the number of the stream:

prngInit(1) // Generate from the first stream
prngInit(2) // Generate from the second stream
.
.
.

After that call, you can generate draws using runif, rnorm, etc. as always. If you call prngInit with the same stream number, you’ll always generate the same random draws. Note that prngInit takes an optional second argument that sets the seed. There is typically no value in setting that to anything other than the default, but it doesn’t harm anything to do so, so it’s an option if you want it.

Additional Details

The above has described all you need to know. I’ll provide you here with a few details on R in case they’re of interest.

You can get the current seed from the variable .Random.seed. What that returns will depend on the generator in use. The function set.seed can be used to convert a single integer value into a seed appropriate for the generator in use. It’s easy to see this if we play with some code. Start R (which is important to replicate these results) and check the current RNG state.

.Random.seed

That returns

Error: object '.Random.seed' not found

Okay, the RNG state has not yet been set. You need to either generate random numbers or set the seed before it’s initialized. Let’s set the seed to whatever state corresponds with the integer 2745:

set.seed(2745)

Now check the current value of the RNG state:

  [1]       10403         624 -2010919067 -1516620574 -1509043461  2143531520
  [7]  -239105535  -630943794 -1918722569  1261767116  -188468131   740534138
 [13]  -813150541  1792613464 -1297864071 -1911085082   494379311  1294628260
 [19]  1755154517  1315405074 -2025123477   946495920  -170513935  -426650882
 [25]  -579151001  1944906876  -919098547 -1600907862   827915043    69526024
 [31]   319514729  1020850454 -1358091105    91213396  -698974907 -2065444542
 [37]  -850334757  1906441568   905299425  1992090158 -1897171753  -598574292
 [43]   427417149  1964838874  1374536595  1910969272    96674393 -1470495162
 [49]  1756271631  1163940612  -983716299  2081567090  2022072907 -1192917744
 [55]  1016946129  -442974882  1514891847 -1928284708 -2055615187  -551447030
 [61]  -124934141  -486887064   791304265  1135437686  1737676671  1023869876
 [67]  1031936805  -139092574   856847547  1248852160  1141633473   177759374
 [73] -1649296457   160603276 -1192855523  1019521082  1312794739 -1929725160
 [79]  1533020729   379983014 -1498351889  1865343076  1246938133  2085688786
 [85] -1059314901 -1053009808   641761713  1777260478  -856331993      208700
 [91]  1529798413  1156138602  1305134307  1547841736  1849899049  -304673322
 [97]  1858073183  1633872148  -350312187  2133301762  1871343003  -955931616
[103]  1291455905  1687099118  -508726633  -112366100    -8257027   926052506
[109]   867564883 -1544809864  1400037913 -1970056442  -969487921  1273896388
[115]    49596917 -1774441934 -2038148085 -1001989168 -1581804143  1747722782
[121]  -885991417   152852636   349102317   201533130  -297250365  -841785304
[127]  -296876023  -756161482  -515080897  -908362124  1218717413 -1471035806
[133] -1225730437 -1875181696  2109217153   523827534  -560555657  2111500108
[139]  -708543523 -1535219462 -1920417229   177412568   166963705    28950886
[145] -1846014801 -1997141212   918274005   485191314 -1950944021   551358256
[151] -1711629967 -1595368322  1186313959 -1867238916   953275085   -91801814
[157] -1287762269    25575304  1229113321  -695604586 -1208977377  -104283180
[163]   -74804027   206316226  -646074533   988285664   -12708511 -1590817874
[169]  1948594263   361963692  -534387267  1354797402    49280787 -2134388424
[175]   383410649  -978231354 -1330856049    -6379388  1763681717  1870062322
[181]   783025611   567734928  -167670447 -1598273826 -1925446201   723696476
[187]   162509997  1653438346 -1847280765   758304488 -1793493047   675487990
[193]  -949755137 -1602045644  -448138587  1345236770  1130952763  1206175296
[199]  -159121087   486952462  -569297097  -355597812  2132688797 -1810830918
[205]  1461951475   845297816 -1910568519  1813130790 -1525882257 -1254099484
[211]  1503165333  -118061230  1771800235   -32733712 -1731956431 -1069604546
[217]  1216070823   415233212 -2048883059   573433834 -1686923165  -223277496
[223]  1674188713  1135707990 -1067532833 -1721672044   593117317   621898626
[229]   -51728101   604582304 -2071864031 -1856389010 -1273844201  -740060308
[235]  -819623555  1384608282  1767616723 -1220915208    -4611687  -697029498
[241]  -841976497  -617483452    60703093   813849522  -759335029  -665466544
[247]  1631274257   604580766  2116875143  1172561436  1742489709 -1551857590
[253]   -48044733  1608056232 -1018386551  -261284426   798558399  -339954700
[259]   255032933  1208768482 -1438983685   674057472  -969955071  -926915890
[265]  -371092233  1395381452 -1464614051   -63165830   886060467   321394520
[271]  2007116153   846158566  1705998383  -825450332 -1633093803 -1724751854
[277] -1672882069 -1081426768   710805745 -1129159170 -1878551961  1256617852
[283]   639302221  -593667926     2794019  -293430008  1033447273  1108206614
[289]  2110440351  -890455724  1045277765 -2110294974 -1953402149 -1725360032
[295]  -729455391  1506948398  -818549801 -1761688020 -1606357699 -1924721958
[301]  -793171309 -1241280840  2110824793  -107235002 -2090734833   426245636
[307] -1640981195 -1038183310 -1804031669 -1367121904  -886784815  1188221022
[313]  1002676551  1814020316  -216753107  1335646474   163761923 -2071597976
[319]  -660105399 -1741956490  -273944961 -1773572428 -2111780315 -1465204574
[325]  2099674043 -1479240768  -998567743 -1490602098   264745655  2041866124
[331]   105187101 -1916785862  1884196723  2074392088   373098809  -242137178
[337]   429903343  1885080420 -1314049259  1180629202   629271083 -1944603792
[343]   377970865  1258449598 -1692852185 -1712866756 -1119801843   177572202
[349] -1692177437  1941629896   376436521 -1637941034 -1510700705  -651504620
[355]  -400238587 -1669448446  -147721061  1896333088 -1492603743  -547919378
[361] -1286674025 -2119911188  -415756035   297759642  1657300051 -1411150472
[367] -1059102439   736626182  -148823857 -1258239804  -996755211  -854880974
[373]  1436392203   723498704  -612502383   500774174   599789319  1888904092
[379]   790147053 -1482626614  1367636163  2046401320  -185972983  1279219510
[385] -1654877121  1256778100 -1177430555  1254746466   233561467   -40199552
[391] -1993983871   -50672562   495161463  -517490100   194120413 -1185092614
[397]   324970803   -90696488  2041555193   104330342  -962731089  -189909468
[403]   -26923307   154947986  -956056597  1249077808  -452953999  -537972866
[409] -1485804057   948157692 -1357700659  1289124394  -434244189 -1055262072
[415]  -301037847  -446374506 -1365504225  -824463660  2090845125 -1398422078
[421]  1905014363  1113925088  1942729825  -961978706   236824407  1989503916
[427]  -137694019 -1330604966   155803155 -2019931080 -1197088551   706286278
[433]   248387215  1757172612 -1030712139 -1173797390 -1209250613 -1796551280
[439]  -100209583  2111592926  1407335623  -435698084  1604879277 -1404159350
[445]   774365827  -554432024  -137054519  -110652426 -1905591809  1952130100
[451]  -234446427  -953560542  1850408763   541024576  1810964545  -822402802
[457] -1596641737  -867840756  -329593187 -1345164102  -406813965  -557698152
[463]  1908017337 -1927061210   845789551  1988305124 -1332680043 -1433769390
[469]    42945963 -1587683088  -578203599 -1338461122 -1295156313   427458492
[475]   525391245    69217002   450510691  -715142840 -2076911959  1675590230
[481]  -847162145  1992247700   394162053 -1418850686  -329238501  1677806752
[487]  1921940513  1955074926  1298277655   412147308  -440821635   -86346470
[493]  1845237715  -135804168   350494873  1894702982  1881721935 -1353015740
[499] -1545719691 -1311261006   488947339  -224091056  1316987921   -73646434
[505] -1444271481   523495708 -2004609171   509889354 -1184035773   305477800
[511] -2128157047  1281659062  -661184577  1029709556   645868901  1988786914
[517]  1879302395  -964499456 -2070165503  -504875570  -411268105  1036951500
[523] -1671474595  1642114426  1999903923   920851032 -1910757255  1662225894
[529]  -490516689  -795161692 -1276090795 -1291238638   434413419  -141092944
[535]   132245489 -1331758850  2062567783  -276036996  -239449779  1362271146
[541]   857229603  1867274248  1487070825   646895382  -127640929  1536533588
[547] -1803494587  1368856386   426637787  -325307552 -1693383711   229869614
[553] -1629723945  -900263636 -2067530691  1090328026   -90140269  1804339640
[559]  1163534425  1026124870 -2131672561 -1013208828   876579893 -1657342094
[565] -1492717493   185416464 -1062724655  -338107554 -1013458873   786090972
[571]  1835756333  2124618762  -854329853   847062888  -257894839 -1309258378
[577]  1369507199 -1866999372   318470437  1907090338 -1529445701  1730490048
[583] -1427755071 -1165882738   -12998217  -125685108  -815819235 -2142785990
[589]   192509555  -776294120   513148985   617118374   493528303 -1649068444
[595] -1270636011  1803082706   247705899  1943998064   734674865 -1780351554
[601]  2107168551   762856764  -904954611   349031530  -392686877   228566728
[607] -1424443863   -97324074  -452648865  -937509100 -1889073403   400613378
[613]  1786084251 -1292510688 -1525462111  1995160814  -263429993 -1364720660
[619]  1755979773 -1814530406  -654914733   289867896  2043142169 -1953974010
[625]  1726445519 -1606454332

Wow. The state of the default RNG is represented as 626 integers. If you wanted, you could save the current 626 integer state, generate some random draws, reset the state to those 626 integers, and then draw the same numbers.

To save the state:

rngstate <- .Random.seed

Generate some draws:

runif(5)

That returns

[1] 0.39984780 0.04520027 0.40373013 0.02144889 0.02943068

Now take some more draws:

runif(5)

and you’ll get different values. Let’s reset the state:

.Random.seed <- rngstate

And generating numbers as before:

runif(5)

we get the same vector:

[1] 0.39984780 0.04520027 0.40373013 0.02144889 0.02943068

However, we know that set.seed(2745) corresponds to that RNG state:

set.seed(2745)
runif(5)

And we verify that we get the same draws:

[1] 0.39984780 0.04520027 0.40373013 0.02144889 0.02943068

So that’s really all there is to it. The difficult thing when working on multiple processors is to set the RNG state appropriately to deliver independent streams. You could do set.seed(101) on the first processor, set.seed(102) on the second, and so on, but the draws would almost certainly be correlated, which would invalidate the simulation.

The magic of the function nextRNGStream is that given the current RNG state, it provides you with a new RNG state that is sufficiently far away that you can take random draws that are independent of the other streams. Thus, with appropriate calls to nextRNGStream, we can set the RNG state in ways that lets us take valid draws on multiple processors simultaneously. It is possibly surprising that the L’Ecuyer generator state is summarized with only six integers. If you want, you can save those six integers and restart the draws from that point in the current stream anywhere else in your program.


Index    Repo