Donate Eightomic iconEightomic

Rand

Replace rand() with eightomic_rand() for faster speed in security-compliant C/C++ systems.

Algorithm

Source

#include <stdint.h> struct eightomic_rand_s { uint32_t a; uint32_t b; }; uint16_t eightomic_rand(struct eightomic_rand_s *s) { s->a = ((s->a << 13) | (s->a >> 19)) ^ s->b; s->b += 1111111; return s->a; }

License

It's free and open source.

Reference

eightomic_rand() is the randomization function that accepts the following argument.

1: s is the struct eightomic_rand_s pointer with 2 32-bit unsigned integers s->a and s->b. Each must be initialized with any combination of randomized values including 0.

The return value data type is uint16_t.

It returns a 16-bit unsigned integer pseudorandom number result.

Explanation

It's the fastest deterministic, one-at-a-time PRNG that passes 8MB stdin16 PractRand tests with 16-bit numbers.

It's faster and safer than every other evaluated C/C++ standard implementation of rand(), including the C99 standard example implementation.

Unlike the other optimal RNGs from Eightomic, eightomic_rand() is built specifically for C/C++ to replace all rand() function calls, although it's still portable to other programming languages.

It has a decent period, fast speed, no broken number cycles and relatively-strong statistical test results.

Furthermore, POSIX random() and /dev/random are still too slow in practical instances where rand() is used.

rand() has no memory space guarantees and POSIX random() allocates a large amount of memory with 31 long integers.

eightomic_rand() guarantees a minimum cycle of 2³² 16-bit numbers for POSIX compliance by summing an odd-numbered constant to s->b, resulting in 2³² different numbers to XOR with s->a.

Eightomic tested a modern, widely-used implementation of rand() that passes only 512KB PractRand tests.

Furthermore, Eightomic tested a modern, widely-used implementation of POSIX random() that passes only 1MB PractRand tests.

Comparatively, eightomic_rand() passes 8MB PractRand tests portably, although statistical analysis is irrelevant to medium-period PRNG security.

The standards-defined minimum range of rand() numbers is from 0 to 32767.

Comparatively, eightomic_rand() generates twice the range of numbers from 0 to 65535.

The jump-ahead moves number cycles forward by 2³² numbers by incrementing s->b and using the same initialized s->a value.

Furthermore, s->a is adjustable whenever offsetting each fixed-length full number cycle is necessary.

The following jump-ahead demonstration proves randomized distribution qualities among different starting points that iterate through s->b.

#include <stdint.h> #include <stdio.h> struct eightomic_rand_s { uint32_t a; uint32_t b; }; uint16_t eightomic_rand(struct eightomic_rand_s *s) { s->a = ((s->a << 13) | (s->a >> 19)) ^ s->b; s->b += 1111111; return s->a; } int main(void) { struct eightomic_rand_s s; uint32_t cycle_starting_point = 11111; unsigned char i = 0; unsigned char j; while (i < 3) { s.a = 11111111; s.b = cycle_starting_point; printf("First 10 Numbers From Starting Point %u\n", cycle_starting_point); eightomic_rand(&s); i++; j = 0; while (j < 10) { printf("%u\n", eightomic_rand(&s)); j++; } if (i < 3) { printf("\n"); } cycle_starting_point++; } }

The following output demonstrates the first 10 numbers from 3 different jump-ahead cycles.

First 10 Numbers From Starting Point 11111 22917 45274 21854 13894 12695 1474 35218 37254 7120 41080 First 10 Numbers From Starting Point 11112 47492 37081 13023 5825 53256 58691 43541 32315 47967 17349 First 10 Numbers From Starting Point 11113 39323 28888 4700 31040 61581 17660 19092 23740 17618 58190

Seeding

Every PRNG needs to be initialized with random seed integers, but generating randomness from external noise and system clock timestamps can cause running processes to fail from unavoidable errors.

To increase fault tolerance, Eightomic provides the only cloud TRNG API that generates theoretically-valid random numbers as a fallback option for try {} catch() {} sequences.