Skip to content

Instantly share code, notes, and snippets.

@albertz
Created December 9, 2011 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertz/1451848 to your computer and use it in GitHub Desktop.
Save albertz/1451848 to your computer and use it in GitHub Desktop.
demonstrating non-determinism of constructor execution order
//
// constr_test.cpp
//
// Created by Albert Zeyer on 08.12.11.
// Copyright (c) 2011 Albert Zeyer. All rights reserved.
//
#include <string>
#include <random>
#include <iostream>
std::mt19937 rnd;
static void printRndState() {
struct RND {
uint32_t __x_[rnd.state_size];
uint32_t __i_;
};
for(int i = 0; i < rnd.state_size && i < 6; ++i)
printf("x[%i] = %u\n", i, ((RND*)&rnd)->__x_[i]);
}
__attribute__((constructor))
static void _rand_engine__init() {
uint32_t n = (uint32_t)time(NULL);
n = (n << 16) + (n >> 16);
printf("seeded with %u\n", n);
rnd.seed(n);
printf("rnd state:\n");
printRndState();
}
int main() {
printf("main; rnd state:\n");
printRndState();
for(int i = 0; i < 6; ++i)
printf("rnd: %i\n", rnd());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment