Created
August 8, 2016 05:32
-
-
Save alpha123/6f02a602c9eaaaf42d64f8b93e65b509 to your computer and use it in GitHub Desktop.
Guts of a random number generator based on cellular automata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <inttypes.h> | |
#include <limits.h> | |
namespace w30 { | |
typedef uint64_t state; | |
typedef uint32_t uint; | |
state step(state ca) | |
{ | |
state ca_next = 0; | |
for (state i = 0; i < sizeof(state) * CHAR_BIT; ++i) | |
{ | |
ca_next ^= | |
(-(30 & (1ull << ( | |
7 | |
& (ca >> (i - 1) | |
| ca << (sizeof(state) * CHAR_BIT + 1 - i))))) ^ ca_next | |
) | |
& (1ull << i); | |
} | |
return ca_next; | |
} | |
template<typename Int_Type, unsigned int ...Columns> | |
state extract_and_step(state ca, Int_Type out[sizeof...(Columns)]) | |
{ | |
uint8_t cols[] = { Columns... }; | |
for (size_t i = 0; i < sizeof...(Columns); ++i) | |
{ | |
out[i] = 0; | |
} | |
for (uint32_t i = sizeof(Int_Type) * CHAR_BIT; i--;) | |
{ | |
for (size_t jj = 0; jj < sizeof...(Columns); ++jj) | |
{ | |
out[jj] ^= (-((ca >> cols[jj]) & 1) ^ out[jj]) & (1ull << i); | |
} | |
state ca_p = ca; | |
ca = 0; | |
for (state ii = 0; ii < sizeof(state) * CHAR_BIT; ++ii) | |
{ | |
ca ^= | |
(-(30 & (1ull << ( | |
7 | |
& (ca_p >> (ii - 1) | |
| ca_p << (sizeof(state) * CHAR_BIT + 1 - ii))))) ^ ca | |
) | |
& (1ull << ii); | |
} | |
} | |
return ca; | |
} | |
template<typename OutputIter> | |
state fill(state ca, OutputIter first, OutputIter last) | |
{ | |
/*uint out[5]; | |
while (first != last) | |
{ | |
ca = w30::extract_and_step<uint, 10, 20, 30, 40, 50>(ca, out); | |
*first = out[0]; | |
if (++first == last) break; | |
*first = out[1]; | |
if (++first == last) break; | |
*first = out[2]; | |
if (++first == last) break; | |
*first = out[3]; | |
if (++first == last) break; | |
*first = out[4]; | |
++first; | |
}*/ | |
while (first != last) | |
{ | |
uint out[1]; | |
ca = w30::extract_and_step<uint, 10>(ca, out); | |
*first = out[0]; | |
++first; | |
} | |
return ca; | |
} | |
/*class wolfram30 { | |
private: | |
w32_state state; | |
public: | |
typedef uint result_type; | |
static result_type min(); | |
static result_type max(); | |
result_type operator(); | |
};*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment