fork(2) download
  1. #include <stdio.h>
  2. #include <inttypes.h>
  3. #include <sys/time.h>
  4.  
  5. /* Headers for the algorithms below */
  6. uint64_t sm_s;
  7. uint64_t xoro_s[2];
  8. uint64_t next_xoro(void);
  9. void jump_xoro(void);
  10. uint64_t next_sm();
  11.  
  12. /* Just a little code to exercise the below methods. */
  13.  
  14. int main(void) {
  15. // Initialise first seed using a semi-random input from the current wall
  16. // clock.
  17. struct timeval tv;
  18. gettimeofday(&tv, NULL);
  19. sm_s = tv.tv_sec;
  20.  
  21. printf("Generating random sequences for the SplitMix algorithm.\n");
  22. uint64_t seeds[5];
  23. for (int i = 0; i < 5; i++) { seeds[i] = next_sm(); }
  24.  
  25. for (int i = 0; i < 5; i++) {
  26. printf("Initial seed: %"PRId64"\n", seeds[i]);
  27. sm_s = seeds[i];
  28. for (int j = 0; j < 20; j++) {
  29. printf("%"PRId64"\n", next_sm());
  30. }
  31. printf("\n");
  32. }
  33.  
  34. printf("Generating random sequences for the xoroshiro128+ algorithm.\n");
  35. for (int i = 0; i < 5; i++) {
  36. xoro_s[0] = next_sm();
  37. xoro_s[1] = next_sm();
  38. printf("Initial seed: %"PRId64" %"PRId64"\n", xoro_s[0], xoro_s[1]);
  39. for (int j = 0; j < 20; j++) {
  40. printf("%"PRId64"\n", next_xoro());
  41. }
  42.  
  43. xoro_s[0] = next_sm();
  44. xoro_s[1] = next_sm();
  45. printf("Initial seed: %"PRId64" %"PRId64"\n", xoro_s[0], xoro_s[1]);
  46. for (int j = 0; j < 5; j++) {
  47. jump_xoro();
  48. printf("Jump %d: %"PRId64"\n", j + 1, next_xoro());
  49. }
  50. printf("\n");
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56. /* Written in 2015-2016 by David Blackman and Sebastiano Vigna (vigna@acm.org)
  57.  
  58. To the extent possible under law, the author has dedicated all copyright
  59. and related and neighboring rights to this software to the public domain
  60. worldwide. This software is distributed without any warranty.
  61.  
  62. See <http://c...content-available-to-author-only...s.org/publicdomain/zero/1.0/>. */
  63.  
  64. static inline uint64_t rotl(const uint64_t x, int k) {
  65. return (x << k) | (x >> (64 - k));
  66. }
  67.  
  68. uint64_t next_xoro(void) {
  69. const uint64_t s0 = xoro_s[0];
  70. uint64_t s1 = xoro_s[1];
  71. const uint64_t result = s0 + s1;
  72.  
  73. s1 ^= s0;
  74. xoro_s[0] = rotl(s0, 55) ^ s1 ^ (s1 << 14); // a, b
  75. xoro_s[1] = rotl(s1, 36); // c
  76.  
  77. return result;
  78. }
  79.  
  80.  
  81. /* This is the jump function for the generator. It is equivalent
  82.   to 2^64 calls to next(); it can be used to generate 2^64
  83.   non-overlapping subsequences for parallel computations. */
  84.  
  85. void jump_xoro(void) {
  86. static const uint64_t JUMP[] = { 0xbeac0467eba5facb, 0xd86b048b86aa9922 };
  87.  
  88. uint64_t s0 = 0;
  89. uint64_t s1 = 0;
  90. for(int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
  91. for(int b = 0; b < 64; b++) {
  92. if (JUMP[i] & 1ULL << b) {
  93. s0 ^= xoro_s[0];
  94. s1 ^= xoro_s[1];
  95. }
  96. next_xoro();
  97. }
  98.  
  99. xoro_s[0] = s0;
  100. xoro_s[1] = s1;
  101. }
  102.  
  103. uint64_t next_sm() {
  104. uint64_t z = (sm_s += UINT64_C(0x9E3779B97F4A7C15));
  105. z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
  106. z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
  107. return z ^ (z >> 31);
  108. }
  109.  
Success #stdin #stdout 0s 2116KB
stdin
Standard input is empty
stdout
Generating random sequences for the SplitMix algorithm.
Initial seed: 1513817156475045394
-3626855871036418500
5037142008337821566
-3562918846162485440
6147600479854282459
-2107227943531753716
7305139801090763158
45944157781804797
-7238041443203849113
4935327494810927
-7465686127882598073
878344852229720568
3574408971783286528
9214682511526103307
729294334327866990
7597411072756597084
-2629190600061795611
-7244729470018064912
-7593388130241446607
1606672671596115209
-962285391878890975

Initial seed: -3624042228962942470
-8093141376403486867
-1660797054317164888
-5301483877711544407
1053859684402764631
-8809592461628447777
-56351121158468066
-5516092378996299582
1478019966702993026
-6961362406803094976
4527174060234373935
-6985005261186685921
-2631743966849514590
-9000478326812591951
-6592178640585358827
-1520793705359419990
8438955686061858502
3127114024932263503
2969998919417794721
-400589350608139551
3909748544636780242

Initial seed: 3531207199216388937
-3206994160392332939
-4012469958504412667
7688722805700151260
-8186447116185818914
9119627738804668866
6465616336965940866
7272417000192644268
2400378858552795454
9211185551331753954
6324808756497678386
-596204120810283059
-4204049282940173092
2993640038985319438
-9046652693398636704
-1927112923864687358
7331671517499201742
-2549640936755132433
-7358605727140964702
4727220835533094256
1137108542316277383

Initial seed: 608525831176989228
7458925378891065981
-4668862760993682703
145865224688153713
7619562588402593909
8947804235711737461
652799901022242297
-4132423561366547548
-108239521146601457
4720074345220598289
-3704014221621206302
1089816034498358065
-1537844168084212989
8376087376569079188
4709888078107164101
-5917848797594813537
8659727343914311863
-2264127904877668051
9185121410365109402
-6970381369069289732
-3337446517345151332

Initial seed: 2243454288033855382
-5828467925148123703
-5879922502433020984
-8558671607890461517
-3891284283703580998
-2710682958278085780
-744668874983046469
1008577969107082690
7753778749835104823
-6252953929927214019
-736347964098444110
-7561126437727570687
-5957881925139424809
7582844502024937043
-1411151789159259645
-2903941993630572853
-8206466541304161149
4976485895977455496
-5374008580841091950
1572495047076487624
-4112667763505259621

Generating random sequences for the xoroshiro128+ algorithm.
Initial seed: -4823001311516472710 5949705871595575025
1126704560079102315
8796220148548794092
-4351969099738545414
1115233890897963686
-341761134134357137
561071025051741906
7204848339415919354
-5077512656280648866
-7692946770369052564
-5063691591573174986
-8897635170497897013
-4199644625846217191
-3544348651434211969
-8079455476894169506
2282080123371869556
911121917961546834
6723482648633717420
-4958475521373389720
-3920040866770853183
-4755577245976293940
Initial seed: 1102771779858531533 643457051388719267
Jump 1: -1121066719408722450
Jump 2: 4310360820389066569
Jump 3: -4735575553454220855
Jump 4: 2031889585379281199
Jump 5: 9076795753267445372

Initial seed: -8884722907433852130 8146936488925077760
-737786418508774370
6831977746526376659
-734363475961070821
3788769925105748835
3799516942461692233
8804496836120406215
4502452080417126604
5209256213827142104
-1703926729889892294
-1182984859477871877
-8258912817497329266
5439129394363390135
-2332636081785475949
3894877403224897644
4134836962150761698
-4211433016749793336
-7811640793330326446
-8425551450224688233
-2263292161856299489
4461705904392488931
Initial seed: -5344967088887150224 5818293704100131212
Jump 1: -85059822157568920
Jump 2: 676681078346374995
Jump 3: 8588231856713208773
Jump 4: 2458078194507175768
Jump 5: 5395811063313778175

Initial seed: -3257375370828617351 -8531679432501196045
6657689270379738220
9222059235171711446
-4201421472743020540
-1739444642814636745
-4973628781447893868
2949181541447815382
1026652562213478519
7697981051981754047
-5336402384342581149
-1668375935023408037
4890366791825267198
6451536666718856816
948536817248600930
-4696574786751313290
5504654956598266192
-4872348553376800030
4123719448275272283
-9077443904670060386
3174136521224631338
-1292006535315758191
Initial seed: 3474006459889022751 8747996624229533430
Jump 1: 1494822226157619902
Jump 2: -3569493440756261250
Jump 3: -5177630526149539193
Jump 4: 1778474867892139208
Jump 5: 2791801284711883546

Initial seed: 9097977738086587765 3108145906336681618
-6240620429286282233
2267394554003498845
-4852688425837652380
167972505769480039
1359621195529982136
1246879650983237670
6867699725247969716
-654784861732131297
4104952218497892293
-7942884659741072784
-733604084842556135
-685596469640985328
-5625263263866414292
890001196481155838
-2704766674184838333
-7147039749947577753
-7322299218734396026
2284453502422000668
-3635723251788242046
4518012520836113769
Initial seed: -6846533767144302270 -1576422842483684744
Jump 1: 5310437083479555798
Jump 2: 9128934508599744339
Jump 3: -7577489496686026435
Jump 4: 4503525820096706339
Jump 5: 8130558390339035774

Initial seed: -7309683336934465458 2484182486618374754
-4825500850316090704
6727637936987884581
3038070578391046784
3608526543373619137
-2570440528851677431
-3486267192874186600
-5765880127275252188
5837093774754120939
2788900921353569322
6387034569474398275
5381503650835844894
2374988773823429462
-8528864665156310533
1978884599989166701
888761003258314467
2541457442086701700
-8224173321358362540
-5561700290344507424
-7520354970005942974
3156150446074668489
Initial seed: -7863438128679380534 -4356936658572020450
Jump 1: 7130612728359616740
Jump 2: -7676912791371400518
Jump 3: 4616951799952053638
Jump 4: 8431453370959560637
Jump 5: 8535454460642049136