-- Leo's gemini proxy

-- Connecting to thrig.me:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini

More Random Things


The Common LISP random number generator, at least in SBCL, is prone to spam perhaps excessive amounts of state. This state is probably good if you're doing long-running simulations where the RNG repeating too soon would be bad, but for other use cases, well, no news is good news.


    * (make-random-state t)
    #S(RANDOM-STATE :STATE #.(MAKE-ARRAY 627 :ELEMENT-TYPE '(UNSIGNED-BYTE 32)
                                         :INITIAL-CONTENTS
                                         '(0 2567483615 624 2147483648 4285120072
                                           790625551 806089414 643877523 1398349795
                                           1003288948 3376820311 1251311338
                                           3638470350 1718892353 1185154100
                                           3359924261 515488018 381377806 322159287
                                           3812999515 3225400745 2597786072
                                           4233373679 2687758388 2830647210
                                           2118189795 3198981874 1916482668
                                           3024808960 2429435080 3912451820
                                           3020611691 3236731632 94393848
                                           1979246812 3703957079 1707533127
                                           1316047713 396258217 982224471
                                           3930783048 2130080730 2252875951
                                           1584372533 1030243661 996233217
                                           498310445 945602594 1456473441
                                           1697245719 2666441129 2760993133
                                           2632902673 970769671 1649780465
                                           3510841812 2458234640 4270931870
                                           1646748023 952669647 817164534
                                           1562599298 480437400 3667222141
                                           2028732837 4237354694 1000414538
                                           467263198 1967150664 1303887997
                                           2389236246 3245374476 1651029302
                                           1644168880 3060324617 2552591437
                                           4087177518 720144015 3026818892
                                           2499790443 210157217 90639555 225838209
                                           3164034997 3763165098 810383068 61039420
                                           3185906301 2649349239 2382756530
                                           2968146797 4229761360 3379716700
                                           4254762986 2851435914 713059131
                                           4230934772 1996489676 1759090856
                                           3876910842 2177311075 720902797
                                           1620425672 1395620575 820235657
                                           3399361250 1820022406 3411189300
                                           2075419057 3027160258 3007456140
                                           832899161 4260016341 275807696
                                           1019825708 4109951726 1647706143
                                           2620583305 3294892591 2640955962
                                           1765255940 98734299 4051772382
                                           1847893553 2588283793 1733778977
                                           1107143179 641029886 3644107597
                                           1685637515 2306680536 3621725066
                                           2114363064 4164418800 3172571868
                                           1979334571 4103765488 867623714
                                           2533085414 2725890402 495267924
                                           1944721839 2406955579 1356041298
                                           2494185188 2498843554 2463937299
                                           1143711601 3089873800 1299534847
                                           1803392553 2820123383 3403602838
                                           660383412 2599671748 2247830118
                                           1178726849 221832501 1493205903
                                           2033227709 1781607788 1443647761
                                           1653138778 2613098745 586925342
                                           2542644716 2857264794 421861946
                                           465545362 3004833968 4067646280
                                           521184539 812879335 3197835686 623634686
                                           3297738546 201647149 1842767724
                                           3833007083 558819394 3444700256
                                           1470076023 459797534 4214663824
                                           2761738451 1319615351 475583632
                                           1791943318 3507833806 3748757250
                                           2693286101 2078209049 530640521
                                           3530833126 3521715094 2370258381
                                           4027459218 4223585235 1386814281
                                           1395540440 3112005942 3800041797
                                           1347365639 665423763 628843472 749801773
                                           574886928 397574748 2769983639 971122002
                                           3733754590 837952908 3866824979
                                           213053338 2292463172 1859806870
                                           1275364992 1559242569 3787986740
                                           2254088069 568383155 4050287820
                                           2189979846 1569864381 606291185
                                           2853782318 3480789009 4179848204
                                           3185723743 186323670 3696866371
                                           2052508568 172592184 980200051
                                           2665981826 3151238806 931492153
                                           328636017 3894988017 2940813625
                                           2724299361 3230820073 2468886937
                                           2970883478 751462797 3206425310
                                           306117684 1588493215 1515372742
                                           1903608772 1765775884 643982141
                                           3638021045 3237245339 4151158129
                                           2953787103 3941237774 1121322567
                                           468885759 3867821095 247024094
                                           4212446595 1539163746 3882670501
                                           2122477068 4291659264 3911439598
                                           155614296 215244114 1189094627
                                           1309074575 2135609211 740292611
                                           991571429 16021390 3272609427 3972988995
                                           1155531950 2928666457 3117410289
                                           3650085444 1397771904 1035411419
                                           4275743913 3072451362 219135819
                                           3480623232 1536052877 1116934529
                                           365567360 3301081424 756257943
                                           1144423689 259400196 528144896
                                           4072693846 2840797382 3519077589
                                           2199804170 1501895764 3473084781
                                           3723938681 1794670575 2645056437
                                           2871007967 3255646344 2129079576
                                           1557543842 2064919772 2650092597
                                           3387637223 3000796502 3787884647
                                           870943834 4292752727 1468556023
                                           2507036077 258637600 2320022570
                                           1932350766 1815354667 3048995666
                                           1332969764 2932084897 353502737
                                           1586489545 2143588842 3921748618
                                           4133404089 1249578871 2316141723
                                           2431447177 2356763202 1017720156
                                           527879690 3550242309 779567636
                                           2329348272 3861271553 613325894
                                           513151275 380352027 489117241 3357082410
                                           3560494942 3963089040 1974144464
                                           2221342044 349320754 38957239 946698155
                                           1202283893 3179465607 2893758968
                                           3134716057 389370128 1573553891
                                           3644687777 2109664463 3929472705
                                           1779241652 3595484528 349358706
                                           3818515217 1317249920 3908308198
                                           896951312 4002133369 3359781843
                                           2822948826 2464329025 3440026567
                                           2854376471 1156313020 1554886326
                                           2665423619 1910313684 332452893
                                           2013928375 2459599358 2967923163
                                           3017082786 2787307516 986240091
                                           4011390527 2824796302 352726291
                                           448684034 2009073246 2643478771
                                           533338937 504751959 1648868066
                                           2451495190 2100892595 427139187
                                           2936874216 2677898043 2062010089
                                           3251839938 3112471858 658440192
                                           3808318009 1507963410 1401851942
                                           110520407 299420730 1527947494 146587081
                                           118480894 786964507 4282138417
                                           1253497401 2309640565 382328599
                                           3510902269 1762806213 1162946753
                                           2522644240 4054209770 2391504131
                                           4162246690 871149892 2676140192
                                           474142602 2903751253 3562900988
                                           1528797149 2974295391 4029576741
                                           2209786432 325120225 2849390297
                                           1973010842 2369908729 2019168403
                                           2590089101 60719547 1512643724
                                           1427327694 2644347692 1104130949
                                           3657866776 2121515211 747270204
                                           3423913508 529177020 1952564873
                                           4052123724 969882260 4175373180
                                           1885702646 2999721028 2177180009
                                           1720323831 398897502 1389833894
                                           308868655 2231714749 635860963
                                           2469573079 2554251694 1417624361
                                           2600574645 3809379778 1180571536
                                           19727870 525004143 1397533911 357769304
                                           457893049 3018314168 3251528088
                                           2089823621 1793540182 3500073771
                                           4179359099 1281669975 162390953
                                           1004853581 2624390318 1551086394
                                           1752726256 1840912040 967504941
                                           3631112809 3274127409 1305490885
                                           187878381 3033380819 3411417157
                                           793237282 1496523026 2087088320
                                           2337843954 3430133101 39911119
                                           3153090630 2355762135 2554717092
                                           1809421974 527224848 1361483012
                                           1472627846 1707419841 3662823151
                                           3519552494 1866113149 1647941457
                                           1901938464 3666681490 2657499161
                                           3257354091 3367377304 705485852
                                           260233250 748770219 3425240824
                                           2377562458 1487156635 3360310541
                                           384390307 1606020554 2453789037
                                           3189588590 2549923229 756196601
                                           2906113561 1393105132 273307174
                                           3210511621 2634346581 336219965
                                           1663561528 4121479446 251905475
                                           2751563885 2233858357 1726454249
                                           1314317141 3979795153 3167616183
                                           893237906 2169982043 4157767852
                                           499537055 2619857477 3977413420
                                           1255314986 3784996610 3997349117
                                           1097528373 602277015 1421414521
                                           1968356332 1686345266 2510741972
                                           313271016 3797191644 173309171
                                           1625544062 430322318 3220211152
                                           3690827047 607225526 2711449835
                                           3407804756 2217659787 2973697245
                                           2251797977 1963690477 1077482723
                                           269858689 1346951276 4170766007
                                           1368633827 3168616232 1914989236
                                           342046797 1890849 437767220 2166470542
                                           67006529 525022941 3954814824 2584386564
                                           3329298779 2721196819 2424729857
                                           3004006620 1256684336 3022803899
                                           670165317 3525983737 3264487256
                                           874207999 1841686698 3641865218
                                           1722347056 1277268228 1960487307
                                           27058671 1877015065 1004241290 201293445
                                           1066175046 1064514126 467448476
                                           3133798205 417054319 3659686668
                                           1336434104 3725758337 3623569376
                                           1718370762 1468941092 2442778598
                                           1209620487 3422947888 2933892757
                                           842415338 3774758510 1016191096
                                           4186128331 2091487022 2489831840
                                           2754411346)))
    *

You probably get the idea, readers who are still with me after that. Of course this output can be hidden in various ways;


    * (block nil (setq *random-state* (make-random-state t)) (return))
    NIL

though in some cases you may want to save the state somewhere so you can get back to it. Probably less so in a simulation, and moreso in a game or music-making where something notable happened and you want to reproduce that. Two options come to mind: first, use a different RNG with an easier to save seed, perhaps one that is seeded with a 32-bit integer; second, persist the state to a file.


    (defun random-save (&optional (file "seed"))
      (with-open-file (stream file :direction :output
                              :if-exists :overwrite
                              :if-does-not-exist :create)
        (block nil (prin1 (make-random-state nil) stream) (return))))

    (defun random-load (&optional (file "seed"))
      (with-open-file (stream file)
        (block nil (setq *random-state* (read stream)) (return))))

A problem is that the code (and possibly also a synthesizer) may also be fiddled around with. Maybe use frequent commits into a version control system? A digital synthesizer could expose its RNG via MIDI so that could also be reset to some known state. Or ignore the problem and do not step in the same stream twice.


Testing that the code (probably) does the right thing is good.


    * (block nil (setq *random-state* (make-random-state t)) (return))
    NIL
    * (random-save)
    NIL
    * (loop repeat 40 do (format t "~a " (random 10)))
    1 8 4 0 1 6 3 6 1 1 7 8 3 6 4 5 8 0 5 1 4 2 0 6 5 4 5 7 3 2 1 5 5 4 7 2 7 6 1 6
    NIL
    * (loop repeat 40 do (format t "~a " (random 10)))
    8 4 6 9 8 3 4 8 9 5 1 9 5 4 3 5 0 4 7 5 8 0 7 4 5 1 3 4 8 7 1 7 5 9 1 3 1 1 5 9
    NIL
    * (random-load)
    NIL
    * (loop repeat 40 do (format t "~a " (random 10)))
    1 8 4 0 1 6 3 6 1 1 7 8 3 6 4 5 8 0 5 1 4 2 0 6 5 4 5 7 3 2 1 5 5 4 7 2 7 6 1 6
    NIL

This code (and the tests for it!) should be shoved off into a utility package, probably with other common (for me) random routines like coinflip, one-in-n, shuffle, picking a random element from a sequence, removing a random element, rolling dice, and so forth. Some of such routines can be found in the Alexandria package.


Speaking of putting things into libraries…


See Also


RANDOM related utilities

Alexandria library

Jenkins Small Fast

xkcd://1319

-- Response ended

-- Page fetched on Tue May 21 16:56:59 2024