Skip to content

Instantly share code, notes, and snippets.

@2bbb
Created March 30, 2016 08:30
Show Gist options
  • Select an option

  • Save 2bbb/04b51969e667b129ec1af1e6db1d4e8e to your computer and use it in GitHub Desktop.

Select an option

Save 2bbb/04b51969e667b129ec1af1e6db1d4e8e to your computer and use it in GitHub Desktop.
check order of xorshift32 is 2^32 - 1
#include "ofMain.h"
#include <array>
template <std::size_t size_, typename T>
struct mat {
static constexpr std::size_t size = size_;
std::array<std::array<T, size>, size> body;
std::array<T, size> &operator[](std::size_t index) {
return body[index];
}
const std::array<T, size> &operator[](std::size_t index) const {
return body[index];
}
mat &operator=(const mat &m) {
for(std::size_t i = 0; i < size; i++) {
for(std::size_t j = 0; j < size; j++) {
body[i][j] = m.body[i][j];
}
}
return *this;
}
mat operator+(const mat &m) const {
mat res;
for(std::size_t i = 0; i < size; i++) {
for(std::size_t j = 0; j < size; j++) {
res.body[i][j] = body[i][j] + m.body[i][j];
}
}
return res;
}
mat &operator+=(const mat &m) {
for(std::size_t i = 0; i < size; i++) {
for(std::size_t j = 0; j < size; j++) {
body[i][j] += m.body[i][j];
}
}
return *this;
}
mat operator*(const mat &m) const {
mat res;
for(std::size_t i = 0; i < size; i++) {
for(std::size_t j = 0; j < size; j++) {
for(std::size_t k = 0; k < size; k++) {
res.body[i][j] += body[i][k] * m.body[k][j];
}
}
}
return res;
}
mat &operator*=(const mat &m) {
mat res = (*this) * m;
std::swap(body, res.body);
return *this;
}
static const mat &E() {
static mat *_E = nullptr;
if(_E == nullptr) {
_E = new mat();
for(std::size_t i = 0; i < size; i++) _E->body[i][i] = 1;
}
return *_E;
}
};
struct Z2 {
using type = std::uint8_t;
type b;
Z2() : Z2(0) {};
template <typename T>
Z2(const T &v) : b(v) {};
template <typename T>
operator T() const { return static_cast<T>(b); }
Z2 operator +(const Z2 &x) const { return (b + x.b) & 1; };
Z2 operator +=(const Z2 &x) { b = (x.b + b) & 1; return *this; };
Z2 operator *(const Z2 &x) const { return (b * x.b); };
Z2 operator *=(const Z2 &x) { b *= x.b; return *this; };
};
using mat32 = mat<32, Z2>;
class ofApp : public ofBaseApp {
mat32 l13, l15, r17;
mat32 x32, y;
mat32 ms[32];
std::size_t loop = 0;
public:
void setup() {
for(std::size_t i = 0; i < mat32::size; i++) {
for(std::size_t j = 0; j < mat32::size; j++) {
l13[i][j] = (i + 13 == j) ? 1 : 0;
l15[i][j] = (i + 15 == j) ? 1 : 0;
r17[i][j] = (i == j + 17) ? 1 : 0;
}
}
x32 = (mat32::E() + l15) * (mat32::E() + r17) * (mat32::E() + l13);
ofBackground(0, 0, 0);
ofSetColor(255, 255, 255);
y = mat32::E();
ms[0] = x32;
for(std::size_t i = 1; i < mat32::size; i++) {
ms[i] = ms[i - 1] * ms[i - 1];
}
}
void draw() {
mat32 &m = y;
int w = 512 / mat32::size, h = 512 / mat32::size;
for(std::size_t i = 0; i < mat32::size; i++) {
for(std::size_t j = 0; j < mat32::size; j++) {
if(m[j][i]) {
ofFill();
} else {
ofNoFill();
}
ofDrawRectangle(i * w, j * h, w, h);
}
}
if(ofGetFrameNum() % 12 == 0) {
y *= ms[loop];
loop = (loop + 1) % 32;
}
}
};
int main() {
ofSetupOpenGL(512, 512, OF_WINDOW);
ofRunApp(new ofApp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment