fork 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 2160KB
stdin
Standard input is empty
stdout
Generating random sequences for the SplitMix algorithm.
Initial seed: 429330476795191297
-4499760438948757019
-3783286044747760566
5921691123826138337
7726573596967839364
2031857482519556243
4046561476202313385
-8300602574855583801
-1611316286017403983
-6438655419333882604
6714101511537347795
-902984402503433461
1552164643442015727
-4140483210659301676
-458306364520248782
9142984190635746408
-5866330431600562295
9122844760003950666
-3029634356227147081
-2043026132494734466
-364775155858219508

Initial seed: 3400742389855446717
594734110698583242
-4000542017346468256
210253421838714293
6968360418762324683
7991499170844240007
5391915900964158817
1526249932392659792
623451784993408734
-7798106767484348395
8211700376725973614
3649400279132871502
-8612718683690709667
-7845607281980141623
1111652550029941983
-2777110907551023596
-3684041210425913687
-1568398431146556802
7716954744763483270
-805384401439426908
2705353858783625057

Initial seed: -7400128961281687434
-5210749463880566139
8282308009370419095
-2670781754201747061
6638136583430012134
-4316578185968258902
7737195773245105349
-5491033219633399337
5203997138248309713
-3089638059384503873
-7407182969604828084
8418200156878297410
-7025271615601357189
6027146824526460654
6631488004052387368
-2877310966391580379
3020299825245867753
-4955430406781147411
-2002141593144187846
-7578176534384376504
2469831356785804657

Initial seed: 234592943059528361
-1399913474782993495
7504927357964696376
8561199601481286205
-1238817530514258987
2435171572167785855
8023730446483208568
746184992077736371
-1275481625185171120
510217355990778719
6608751910715384057
-8093819124851585799
6131462065280425778
602710954384932471
-1269396914990334675
1262598715978370282
-7130084442654535255
8896374123733650969
-3288136633900868385
-8962934814274422462
-1766066058636794134

Initial seed: 3308203117054138035
-7861491523441622216
-365404143909602957
-7689958408164000317
-6239891311548242444
7352504845773014848
2124313277151943250
6930547857958111572
-832725508692255890
-6975972806192046078
-3977149174949988833
1200339812449509752
-6207832082058761331
6801324227826349744
-2896947569671930686
674608729663050710
-7522292838568797915
-9158140142507160329
-3355988680814776711
-8658619950956740226
4169011013542433203

Generating random sequences for the xoroshiro128+ algorithm.
Initial seed: 7466763236896258944 -1564294205728215327
5902469031168043617
7198773297087363760
6511019481306438310
6805937670766273472
7422288258887783584
-4110081440968305496
3320553885932104876
9004323854900464289
-8063466066358415725
6934613490556277802
-8696937840573800867
4825238703135643023
-1653493201001060868
42260303558577377
-4626014675938634426
-944508197994376332
-6366473392214075266
3950621334431780796
6787423372391118498
611197869042383294
Initial seed: 990903846483086990 -9148032195894284410
Jump 1: -202913253275002842
Jump 2: -3343919899937856555
Jump 3: 7815278568507025494
Jump 4: -7622830828298576044
Jump 5: 2861384012317217776

Initial seed: -4112066845936433283 3163302063874436343
-948764782061996940
-7821776231016794550
-7030552912602174385
8958716425930328502
-354922957426629560
859132303837298162
-5334998035091375266
-2800784435775762996
7353695532251559551
2814371675410550454
7343007739229141345
-8944151890871223344
5930305798524816002
-4163782670270808330
5979807316683484099
-7092339377833469817
-4661317528092520811
7796919036230843598
-624412798709654714
-2467985928541876216
Initial seed: -4009791646934021264 2482945807292523774
Jump 1: 6862507382354034559
Jump 2: 7011875142185683248
Jump 3: -4496564143832921207
Jump 4: 1853506959186203582
Jump 5: -8638081920607530712

Initial seed: -3556036892712017828 4744313911787442952
1188277019075425124
-2031588697732539858
-8601851069438057733
-1783187797461758034
3375484877072944896
7462292442845328860
-3670405771325518947
2021919734452842993
1602781825889362786
799454451685605819
-1909096564437143223
-1578291332004927205
7027493770310518551
-5377717905298702031
6623838181451763101
-4751789108151407327
1449975645991318649
1351175348912862183
-1115098130549484081
4694343480567463234
Initial seed: 8408836858555572576 -8576334934951868580
Jump 1: -2331108112525860358
Jump 2: -7058988001537110731
Jump 3: -6592145494078489880
Jump 4: -1634515623999943417
Jump 5: 3097860178046766537

Initial seed: 4888108530473458491 1080114599882612163
5968223130356070654
6032985129407534403
4534609269871591120
-1731260568486595776
5758190196341762132
6589264333898394748
1097157270079905485
-4815640155108239881
-2785127221164809533
5690309465150585478
-1655891647747823172
-2223483582408611975
-2333256348386901250
-2079169955617089199
5860842200898697024
-4305100838382793359
2205070721799922809
2040800925900756689
4216004939170149640
-741217424629009490
Initial seed: 1865665657046312887 -2081931776983017860
Jump 1: 6193425956360965668
Jump 2: -5684898831560071200
Jump 3: -2065913535431491189
Jump 4: 9075337997212788543
Jump 5: -5025025827370910366

Initial seed: 1509269885909564309 7133068101378203014
8642337987287767323
4292927883082007017
467007447584346989
2774909934043114559
-5933931602561221033
1230699784193511151
3172874214865991087
5624032393155783
8334028308889261817
6847835692669780223
1066185610557673137
-7713465035237903947
1555430774008560978
6049341261429709507
-3654928520531525839
-2830381161584601530
-1087629377749160679
3005996437783538394
4521566568763510045
-8592592243709414385
Initial seed: 4716662714674336978 -4755215357118135582
Jump 1: 9063123224574765832
Jump 2: -8444699141156829399
Jump 3: -908412126696469887
Jump 4: 5890875775521162114
Jump 5: -3787313022067243265