random123

Counter-based Random Number Generators
git clone git://git.meso-star.com/random123.git
Log | Files | Refs | README | LICENSE

example_seeds.h (2309B)


      1 #ifndef EXAMPLE_SEEDS_H__
      2 #define EXAMPLE_SEEDS_H__ 1
      3 
      4 /*
      5  * This entire file is overkill to allow seeds to be set in Random123
      6  * example and test programs via a R123EXAMPLE_ENVCONF_SEED environment
      7  * variable, mainly to illustrate and test how one might use a user-set
      8  * seed to produce different random streams for different runs.
      9  * None of this code is needed for the correct functioning or
     10  * use of the Random123 library.
     11  */
     12 
     13 #include <stdlib.h> // for strtoul
     14 #include <limits.h> // for ULONG_MAX
     15 #include <errno.h>  // for errno
     16 #include <string.h> // for strerror
     17 #include <stdio.h>  // for stderr
     18 
     19 /*
     20  * The following arbitrary values for sample seeds (used to
     21  * initialize keys and counters in the examples) have no
     22  * particular meaning.  They could equally easily all be 0.
     23  */
     24 #define EXAMPLE_SEED1_U32   0x11111111U
     25 #define EXAMPLE_SEED2_U32   0x22222222U
     26 #define EXAMPLE_SEED3_U32   0x33333333U
     27 #define EXAMPLE_SEED4_U32   0x44444444U
     28 
     29 #define EXAMPLE_SEED5_U32   0xdeadbeefU
     30 #define EXAMPLE_SEED6_U32   0xbeadcafeU
     31 #define EXAMPLE_SEED7_U32   0x12345678U
     32 #define EXAMPLE_SEED8_U32   0x90abcdefU
     33 #define EXAMPLE_SEED9_U32   0xdecafbadU
     34 
     35 #if R123_USE_64BIT
     36 #define EXAMPLE_SEED1_U64   R123_64BIT(0xdeadbeef12345678)
     37 #define EXAMPLE_SEED2_U64   R123_64BIT(0xdecafbadbeadfeed)
     38 #endif
     39 
     40 static inline unsigned long example_seed_u64(uint64_t defaultseed) {
     41     const char *e = "R123EXAMPLE_ENVCONF_SEED";
     42     const char *cp = getenv(e);
     43     unsigned long u;
     44     char *ep;
     45     errno = 0;
     46     if (cp) {
     47 	u = strtoul(cp, &ep, 0);
     48 	if (u == ULONG_MAX && errno != 0) {
     49 	    fprintf(stderr, "strtoul failed to convert environment variable %s=\"%s\" to unsigned long: %s\n",
     50 		    e, cp, strerror(errno));
     51 	    exit(1);
     52 	} else if (*ep != '\0') {
     53 	    fprintf(stderr, "strtoul failed to fully convert environment variable %s=\"%s\" to unsigned long, got 0x%lu\n",
     54 		    e, cp, u);
     55 	    exit(1);
     56 	}
     57     } else {
     58 	u = defaultseed;
     59     }
     60     return u;
     61 }
     62 
     63 static inline uint32_t example_seed_u32(uint32_t defaultseed) {
     64     uint64_t u64 = example_seed_u64(defaultseed);
     65     if (u64 > 0xFFFFFFFFUL /* UINT32_MAX, which clang29 does not have, sigh */) {
     66 	fprintf(stderr, "Warning: truncating seed 0x%lu to uint32_t\n", (unsigned long)u64);
     67     }
     68     return (uint32_t)u64;
     69 }
     70 
     71 
     72 #endif /* EXAMPLE_SEEDS_H__ */