Created
December 9, 2011 14:59
-
-
Save albertz/1451848 to your computer and use it in GitHub Desktop.
demonstrating non-determinism of constructor execution order
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
// | |
// 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