Last active
August 18, 2020 19:12
-
-
Save johannes-riecken/ab4f0d3cf3257566fac2241216c8bdde to your computer and use it in GitHub Desktop.
Reproducing random integer generation in different languages
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
#include <iostream> | |
#include <chrono> | |
#include <random> | |
#include <iomanip> | |
int main () | |
{ | |
std::mt19937 generator (0); | |
std::cout << generator() << std::endl; | |
std::cout << generator() << std::endl; | |
std::cout << generator() << std::endl; | |
// Output: | |
// 2357136044 | |
// 2546248239 | |
return 0; | |
} |
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
import std.random; | |
void main () { | |
Mt19937 gen; | |
gen.seed(0); | |
import std.stdio; | |
writeln(gen.front); | |
} | |
// 2357136044 |
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
#!/usr/bin/perl -w | |
use strict; | |
use feature 'say'; | |
use Math::Random::MT; | |
my $r = Math::Random::MT->new(0); | |
say $r->irand(); | |
say $r->irand(); | |
# Output: | |
# 2357136044 | |
# 2546248239 |
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
import numpy as np | |
np.random.seed(0) | |
print(np.random.randint(0, 4294967295)) # not sure if max value is correct | |
print(np.random.randint(0, 4294967295)) | |
# Output: | |
# 2357136044 | |
# 2546248239 |
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
r = Random.new(0) | |
p r.rand(4294967296) | |
p r.rand(4294967296) | |
# Output: | |
# 2357136044 | |
# 2546248239 |
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
# Don't know who the author is sadly | |
def _int32(x): | |
# Get the 32 least significant bits. | |
return int(0xFFFFFFFF & x) | |
class MT19937: | |
def __init__(self, seed): | |
# Initialize the index to 0 | |
self.index = 624 | |
self.mt = [0] * 624 | |
self.mt[0] = seed # Initialize the initial state to the seed | |
for i in range(1, 624): | |
self.mt[i] = _int32( | |
1812433253 * (self.mt[i - 1] ^ self.mt[i - 1] >> 30) + i) | |
def extract_number(self): | |
if self.index >= 624: | |
self.twist() | |
y = self.mt[self.index] | |
# Right shift by 11 bits | |
y = y ^ y >> 11 | |
# Shift y left by 7 and take the bitwise and of 2636928640 | |
y = y ^ y << 7 & 2636928640 | |
# Shift y left by 15 and take the bitwise and of y and 4022730752 | |
y = y ^ y << 15 & 4022730752 | |
# Right shift by 18 bits | |
y = y ^ y >> 18 | |
self.index = self.index + 1 | |
return _int32(y) | |
def twist(self): | |
for i in range(624): | |
# Get the most significant bit and add it to the less significant | |
# bits of the next number | |
y = _int32((self.mt[i] & 0x80000000) + | |
(self.mt[(i + 1) % 624] & 0x7fffffff)) | |
self.mt[i] = self.mt[(i + 397) % 624] ^ y >> 1 | |
if y % 2 != 0: | |
self.mt[i] = self.mt[i] ^ 0x9908b0df | |
self.index = 0 | |
#test | |
m = MT19937(0) | |
print(m.extract_number()) | |
print(m.extract_number()) | |
# Output: | |
# 2357136044 | |
# 2546248239 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment