├── LICENSE ├── README.md └── ranxoshi256.h /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Documentation 2 | 3 | This is a header-only library, as such most of its functional documentation is contained within the "header section" of the 4 | source code in the form of comments. It is highly recommended that you read said documentation before using this library. 5 | 6 | ## Features 7 | 8 | The ranxoshi256 library provides a ready implementation of the xoshiro256** algorithm, its features include: 9 | 10 | - No hard dependencies besides the standard library, making it fully portable for most purposes 11 | - Endian-proof seed function to allow for synchronized PRNGs across different machines 12 | - Supports various output functions with different ranges and uniformities 13 | 14 | ## Attribution 15 | 16 | You are not required to give attribution when using this library. If you want to give attribution anyway, either link to 17 | this repository, [my website](https://www.slopegames.com/), or credit me as [BareRose](https://github.com/BareRose). 18 | If you want to support me financially, consider giving to my [Patreon](https://www.patreon.com/slopegames). 19 | 20 | ## License 21 | 22 | Licensed under CC0 aka the most lawyer-friendly way of spelling "public domain". -------------------------------------------------------------------------------- /ranxoshi256.h: -------------------------------------------------------------------------------- 1 | /* 2 | ranxoshi256.h - Portable, single-file, PRNG library implementing the xoshiro256** algorithm 3 | 4 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring 5 | rights to this software to the public domain worldwide. This software is distributed without any warranty. 6 | You should have received a copy of the CC0 Public Domain Dedication along with this software. 7 | If not, see . 8 | */ 9 | 10 | /* 11 | ranxoshi256 supports the following three configurations: 12 | #define RANXOSHI256_EXTERN 13 | Default, should be used when using ranxoshi256 in multiple compilation units within the same project. 14 | #define RANXOSHI256_IMPLEMENTATION 15 | Must be defined in exactly one source file within a project for ranxoshi256 to be found by the linker. 16 | #define RANXOSHI256_STATIC 17 | Defines all ranxoshi256 functions as static, useful if ranxoshi256 is only used in a single compilation unit. 18 | */ 19 | 20 | //header section 21 | #ifndef RANXOSHI256_H 22 | #define RANXOSHI256_H 23 | 24 | //process configuration 25 | #ifdef RANXOSHI256_STATIC 26 | #define RANXOSHI256_IMPLEMENTATION 27 | #define RSHI256DEF static 28 | #else //RANXOSHI256_EXTERN 29 | #define RSHI256DEF extern 30 | #endif 31 | 32 | //includes 33 | #include 34 | 35 | //structs 36 | struct ranxoshi256 { 37 | uint64_t s[4]; //PRNG state 38 | }; 39 | 40 | //function declarations 41 | RSHI256DEF void ranxoshi256Seed(struct ranxoshi256*, const unsigned char[32]); 42 | //pastes the given seed of 32 bytes into the generator's state in an endian-proof way 43 | //this allows for consistent results across machines with differing architectures 44 | RSHI256DEF float ranxoshi256FloatCO(struct ranxoshi256*); 45 | //returns a random float in the range [0.0, 1.0) (not including 1.0) 46 | //provides maximum uniformity but only 2^24 possible values 47 | RSHI256DEF float ranxoshi256FloatCC(struct ranxoshi256*); 48 | //returns a random float in the range [0.0, 1.0] (including 0.0 and 1.0) 49 | //uses more of float's available precision at the cost of uniformity 50 | RSHI256DEF double ranxoshi256DoubleCO(struct ranxoshi256*); 51 | //returns a random double in the range [0.0, 1.0) (not including 1.0) 52 | //provides maximum uniformity but only 2^53 possible values 53 | RSHI256DEF double ranxoshi256DoubleCC(struct ranxoshi256*); 54 | //returns a random double in the range [0.0, 1.0] (including 0.0 and 1.0) 55 | //uses more of double's available precision at the cost of uniformity 56 | RSHI256DEF uint64_t ranxoshi256Next(struct ranxoshi256*); 57 | //returns a random uint64 (raw output of the generator) 58 | RSHI256DEF void ranxoshi256Jump(struct ranxoshi256*); 59 | //advances the given generator's internal state by 2^128 calls to ranxoshi256Next 60 | //useful for generating multiple non-overlapping subsequences from a single seed 61 | 62 | #endif //RANXOSHI256_H 63 | 64 | //implementation section 65 | #ifdef RANXOSHI256_IMPLEMENTATION 66 | #undef RANXOSHI256_IMPLEMENTATION 67 | 68 | //function declarations 69 | static uint64_t ranxoshi256Rotate(uint64_t, int); 70 | 71 | //public functions 72 | RSHI256DEF void ranxoshi256Seed (struct ranxoshi256* ctx, const unsigned char s[32]) { 73 | for (int i = 0; i < 4; i++) 74 | ctx->s[i] = ((uint64_t)s[i*8] << 56)|((uint64_t)s[i*8+1] << 48)| 75 | ((uint64_t)s[i*8+2] << 40)|((uint64_t)s[i*8+3] << 32)| 76 | ((uint64_t)s[i*8+4] << 24)|((uint64_t)s[i*8+5] << 16)| 77 | ((uint64_t)s[i*8+6] << 8)|((uint64_t)s[i*8+7]); 78 | } 79 | RSHI256DEF float ranxoshi256FloatCO (struct ranxoshi256* ctx) { 80 | return (float)(ranxoshi256Next(ctx) >> 40)/16777216.0f; 81 | } 82 | RSHI256DEF float ranxoshi256FloatCC (struct ranxoshi256* ctx) { 83 | return (float)(ranxoshi256Next(ctx) >> 32)/(float)UINT32_MAX; 84 | } 85 | RSHI256DEF double ranxoshi256DoubleCO (struct ranxoshi256* ctx) { 86 | return (double)(ranxoshi256Next(ctx) >> 11)/9007199254740992.0; 87 | } 88 | RSHI256DEF double ranxoshi256DoubleCC (struct ranxoshi256* ctx) { 89 | return (double)ranxoshi256Next(ctx)/(double)UINT64_MAX; 90 | } 91 | RSHI256DEF uint64_t ranxoshi256Next (struct ranxoshi256* ctx) { 92 | uint64_t res = ranxoshi256Rotate(ctx->s[1] * 5, 7) * 9; 93 | uint64_t t = ctx->s[1] << 17; 94 | ctx->s[2] ^= ctx->s[0]; 95 | ctx->s[3] ^= ctx->s[1]; 96 | ctx->s[1] ^= ctx->s[2]; 97 | ctx->s[0] ^= ctx->s[3]; 98 | ctx->s[2] ^= t; 99 | ctx->s[3] = ranxoshi256Rotate(ctx->s[3], 45); 100 | return res; 101 | } 102 | RSHI256DEF void ranxoshi256Jump (struct ranxoshi256* ctx) { 103 | const uint64_t jump[] = {0x180EC6D33CFD0ABA, 0xD5A61266F0C9392C, 0xA9582618E03FC9AA, 0x39ABDC4529B1661C}; 104 | uint64_t s0 = 0, s1 = 0, s2 = 0, s3 = 0; 105 | for (int i = 0; i < sizeof(jump)/sizeof(jump[0]); i++) 106 | for (int b = 0; b < 64; b++) { 107 | if (jump[i] & (uint64_t)1 << b) { 108 | s0 ^= ctx->s[0]; 109 | s1 ^= ctx->s[1]; 110 | s2 ^= ctx->s[2]; 111 | s3 ^= ctx->s[3]; 112 | } 113 | ranxoshi256Next(ctx); 114 | } 115 | ctx->s[0] = s0; 116 | ctx->s[1] = s1; 117 | ctx->s[2] = s2; 118 | ctx->s[3] = s3; 119 | } 120 | 121 | //internal functions 122 | static uint64_t ranxoshi256Rotate (uint64_t x, int k) { 123 | return (x << k) | (x >> (64 - k)); 124 | } 125 | 126 | #endif //RANXOSHI256_IMPLEMENTATION --------------------------------------------------------------------------------