This file contains python code for understanding how $Unif[0,1]$ random deviates are generated. The R version contains more discussion. Here we just focus on the code.
Python's default random number generator is the Mersenne Twister, the same as R. It is not possible to easily switch to the Wichmann-Hill generator in python (as it is in R), but we can manually code Wichmann-Hill easily and generate random deviates.
import numpy as np
np.random.uniform(size=10)
array([ 0.3520936 , 0.38494655, 0.30045102, 0.66379145, 0.17434746, 0.48122222, 0.1254412 , 0.29137164, 0.47897745, 0.76517471])
np.random.seed(1234)
np.random.uniform(size=10)
array([ 0.19151945, 0.62210877, 0.43772774, 0.78535858, 0.77997581, 0.27259261, 0.27646426, 0.80187218, 0.95813935, 0.87593263])
np.random.seed(1234)
np.random.uniform(size=10)
array([ 0.19151945, 0.62210877, 0.43772774, 0.78535858, 0.77997581, 0.27259261, 0.27646426, 0.80187218, 0.95813935, 0.87593263])
## python used mersenne twister which has many seeds, complicated
np.random.get_state()
('MT19937', array([2260313690, 348938374, 3392255680, 2909033704, 140638832, 1016917445, 4051655600, 976942074, 1628339371, 932989997, 417988570, 3106230116, 3847402493, 2846838083, 1854065059, 2365406610, 631390710, 3006558680, 1855109059, 230064328, 758538135, 1999313224, 2345696623, 4174662269, 280561112, 1706268812, 4182435209, 1014638053, 610687375, 2331525695, 3432349290, 1302213857, 2461808965, 1211193860, 3120004290, 159403718, 785407708, 1103582039, 2181742160, 4003474818, 3333684546, 2164025542, 3329631014, 3331897623, 44841503, 2124190575, 4103716897, 1985760015, 3231349092, 2579223365, 2045506447, 1684183393, 4105280043, 264622240, 3457386480, 3640204395, 3308050770, 1877785652, 1671495555, 489816736, 3645271096, 1053221445, 4106060276, 3422181599, 2615443735, 1804244509, 3539838858, 3452443235, 2399113344, 1565187405, 2474733055, 1149428025, 3675651662, 3578586687, 2141819878, 3912530560, 3882533229, 502129057, 2262588256, 243511723, 3669928491, 2909147132, 2533094556, 1693913013, 1031455591, 892347669, 1204715320, 2778280540, 3103938066, 1548020698, 2371526184, 2362735796, 601580678, 4163515833, 2286774353, 3385525426, 3435050721, 3859726043, 658564489, 1680167334, 395160378, 1918020476, 2179531800, 750714379, 1277823100, 3279993862, 968375750, 2645945689, 1435905461, 453299951, 2432221527, 2543208725, 977186272, 374115648, 1760836837, 1339597678, 4072942346, 1502786855, 1263608786, 2235649091, 3748764825, 105321286, 3494441207, 889085055, 1548213403, 3930247105, 1261616741, 3701589864, 2730870951, 2328741100, 1549885863, 885362289, 4236685512, 4183414762, 2700385771, 4179975148, 1324969786, 3735228741, 3337696400, 342695004, 1189947554, 799007231, 3479707634, 2754945644, 450211552, 2069028394, 1489498981, 3077105910, 2775627593, 1485941576, 1906706347, 1363343182, 1154927348, 2663740949, 3518726712, 3224543204, 2088074182, 3636799541, 876485687, 1331764607, 1721688505, 2144969088, 4090073983, 1223328334, 1893823894, 4023014570, 2390997307, 722079132, 534353189, 4068461700, 4005844884, 3362478912, 2758321754, 2734444009, 970440841, 3012468549, 1113307510, 4226440206, 4008367189, 2219268773, 870392716, 2499253257, 3083981992, 2008109066, 1151287397, 2169435052, 2384932835, 1612096122, 2171119261, 3321318748, 1317674930, 1449643218, 2113476231, 3313810858, 1519782446, 2630003892, 2464758765, 9731462, 1052415654, 3656592014, 2841565583, 3624077715, 2983921733, 562911616, 1962641375, 614269341, 785306885, 4273760749, 1888303864, 3171945333, 747233216, 368198111, 3813072643, 1724804430, 681854442, 4131373003, 2999398905, 2879312256, 3658689550, 2347669835, 2664829219, 2807075018, 3253655972, 2002544366, 1643713543, 1186919809, 3215199810, 4029160488, 1068185241, 915000036, 2065976610, 4276053157, 3527758033, 2401161367, 842168689, 507969091, 1804685371, 958670079, 2845853863, 2795655679, 229490910, 774557515, 2141840939, 1784889067, 1679680385, 2648417388, 2429705148, 2754293229, 1433256960, 126113575, 3636255678, 3156875032, 3485050346, 2225833886, 477422240, 2922494267, 3447028538, 4123742736, 1624289207, 1362957341, 578091799, 1188249426, 4254745168, 2077134384, 3248839208, 920104920, 777072686, 919500975, 954689369, 3734345697, 963527021, 1627543331, 610865187, 3652041124, 2034149562, 3544763031, 77621747, 4128985850, 1587424817, 1354288442, 3619314760, 1708376797, 326940888, 2058024529, 3459367601, 1470000018, 1095941426, 2700094647, 1369430284, 240943488, 361842083, 961851483, 2191681518, 3308512613, 2054940002, 4265757199, 1166040063, 3479215563, 3448883455, 660622673, 1004507739, 444314655, 3089971362, 3441658735, 2054386395, 721606098, 2808281070, 3988922231, 2981069157, 3187660644, 1700438419, 465094909, 2067097955, 781034920, 3593382873, 221537540, 4058240238, 2649691518, 1464747161, 2171116181, 277760487, 2657734871, 1713714962, 2306668985, 1921083486, 3101885825, 663912034, 2109009255, 1604183668, 752862648, 2154754068, 2146568730, 2346085114, 227251157, 3434674477, 1825639385, 614299416, 1712896225, 115429852, 3468720208, 3812351631, 144413573, 1565979984, 4039450152, 4216138990, 1663623049, 2247050244, 3060868821, 1566337412, 3382686158, 2509623692, 1263563671, 1360200562, 1826970920, 2705971464, 1615712977, 4286498113, 4230861534, 2996730271, 1690375340, 2889722659, 3759201910, 195871346, 657582540, 3854023474, 443002808, 1522982692, 524536023, 4259312476, 1550095226, 4167482166, 156698076, 4237215123, 3869358353, 1811122148, 1187724156, 2737100772, 1406546615, 432196015, 814245354, 4129949113, 1940145834, 1587754779, 3914244363, 2890981831, 2532990725, 4220043234, 922349960, 3682307144, 3519797929, 1872948906, 3421600291, 1356585955, 2660558280, 2593655527, 1829590266, 4071773680, 3568093215, 2602224556, 2629361598, 3225297981, 3639800786, 1400651480, 769725007, 3509817332, 1138316693, 1429898715, 3029690960, 3512041792, 2693047090, 3025060641, 2934039187, 271210632, 1213597215, 466142102, 1489185601, 2431230993, 1940741760, 1856343276, 3695963554, 277495980, 2525997388, 3753281030, 3833241502, 1492571399, 203035164, 2190351553, 3620139539, 2628646633, 2244091324, 1274966498, 3847098676, 958180754, 1263580506, 2803396943, 804048593, 578223533, 3232363764, 3810610989, 2666351419, 274246722, 3045961342, 2654133257, 338915900, 135050623, 3714710897, 1122319361, 1930288394, 1514757446, 2648411551, 2665976818, 1922954710, 2414027436, 1364684265, 395814907, 803208761, 2521936447, 631823100, 570463898, 1034907987, 179273695, 2234303106, 2679028703, 2219522624, 2189595580, 3160356306, 2041672333, 2986536412, 3816979234, 3054356004, 1932200261, 487488630, 2462430590, 3646338000, 1173263836, 542681657, 1746431646, 2016274431, 50704002, 2910955822, 3136959849, 2562376297, 2669796713, 218959494, 2736407903, 2112935337, 2184602348, 3131955204, 1988918897, 1979433465, 111037611, 1307546936, 1620099127, 1388754931, 507698292, 3193492631, 2715688218, 2909281280, 1057165160, 951970226, 1479088468, 2466700726, 110695581, 2588386715, 3894786584, 1363081750, 2562094110, 673223621, 3455478874, 319727983, 3420367804, 2595169198, 3952587382, 841364433, 3277142633, 2615888764, 353016384, 897840736, 884587774, 2356096243, 231721642, 2574185937, 13589462, 865475638, 4107744461, 1653527351, 2649632148, 1167334644, 1569430142, 3609105483, 3463259590, 1179723884, 2115248217, 3982052755, 487211103, 2363841818, 3724666385, 2275560911, 3830737525, 353381279, 1123433786, 1682768477, 2473812269, 1408021729, 1584109860, 4045088634, 1624954728, 1941754630, 4153741554, 3653232275, 1526795240, 3729710413, 332973006, 1697259304, 4217938175, 3876775776, 2901195850, 1611722990, 2548595086, 4265201072, 76991528, 3763912135, 1696021447, 123633128, 225556227, 3703923807, 4055295818, 1989679936, 572855962, 2422414634, 4293793217, 3757525477, 2042976673, 4132414536, 4241580006, 2077062331, 2198064263, 3998557957, 563847915, 2851070600, 3105990049, 2079504127, 1211296335, 61687311, 1982828632, 2130228175, 1557705711, 1212550942, 1205493497, 185279173, 4165883878, 773213171, 344698889, 1395910106, 3707815628, 1334816435, 2620911066, 1935228689, 180053610, 4078401641, 1736554240, 3643702302, 3315857509, 341577669, 2807850657, 391126227, 2467381806, 2838072779, 3039008762, 3826797962, 421136520, 3827508772, 1428234374, 798512555, 1640145905, 2443857604, 1869726726, 374514272, 2743520988, 3451965119, 3983557115, 1676015003, 2941385220, 2985199325], dtype=uint32), 20, 0, 0.0)
We write the Wichmann-Hill random number generator in python.
## set up wichmann-hill parameters
seed = np.array([1234,9779,23188],dtype=np.uint32)
pWH = np.array([30269,30307,30323],dtype=np.uint32)
aWH = np.array([171,172,170],dtype=np.uint32)
(aWH*seed) % pWH
array([29400, 15103, 30293], dtype=uint32)
## wichmann-hill in python
def myunif(n):
global seed ## seed now modifies variable outside function
X = np.zeros([n + 1,3],dtype=np.uint32)
X[0,:] = seed
for ii in (np.arange(n) + 1):
X[ii,:] = (aWH*X[(ii-1),:]) % pWH
seed = X[n,:]
return (np.sum(X/pWH,axis=1) % 1)[1:]
print(seed)
print(myunif(5))
print(seed)
print(myunif(5))
print(seed)
print(myunif(1))
print(seed)
print(myunif(1))
print(seed)
[17691 13803 27638] [ 0.2251844 0.8950292 0.76547244 0.9619798 0.52286985] [4690 3060 8095] [ 0.24475195 0.83582643 0.81010513 0.77349383 0.35721686] [16300 24260 553] [ 0.86632129] [ 2552 20661 3041] [ 0.72237669] [12626 7773 1479]