Skip to content

Instantly share code, notes, and snippets.

@johannes-riecken
Last active July 11, 2020 14:43
Show Gist options
  • Save johannes-riecken/d23ce08585e19af9ce84f7a43ec2b270 to your computer and use it in GitHub Desktop.
Save johannes-riecken/d23ce08585e19af9ce84f7a43ec2b270 to your computer and use it in GitHub Desktop.
Reproducing random floating point number generation in different languages
rand('state', 0);
rand();
rand();
// Output:
// 0.84442
// 0.75795
from random import *
seed(0, version=1)
print(random())
print(random())
# Output:
# 0.8444218515250481
# 0.7579544029403025
import numpy as np
np.random.seed(0)
print(np.random.random())
print(np.random.random())
# Output:
# 0.5488135039273248
# 0.7151893663724195
import 'random'
srand(0)
p rand()
p rand()
# Output
# 0.5488135039273248
# 0.7151893663724195
#include <iostream>
#include <chrono>
#include <random>
#include <iomanip>
int main ()
{
std::mt19937 generator (0.0);
std::uniform_real_distribution<float> dis(0.0, 1.0);
std::cout << std::setprecision(15);
std::cout << std::setiosflags (std::ios::fixed);
std::cout << dis(generator) << std::endl;
std::cout << dis(generator) << std::endl;
std::cout << dis(generator) << std::endl;
// Output:
// 0.548813521862030
// 0.592844605445862
// 0.715189337730408
return 0;
}
using System;
using MathNet.Numerics.Random;
namespace random
{
class Program
{
static void Main(string[] args)
{
double[] arr = MersenneTwister.Doubles(3, 0);
Console.WriteLine(arr[0]);
Console.WriteLine(arr[1]);
Console.WriteLine(arr[2]);
// Output:
// 0.548813502304256
// 0.5928446163889021
// 0.715189364971593
}
}
}
#!/usr/bin/perl -w
use strict;
use feature 'say';
use Math::Random::MT;
my $r = Math::Random::MT->new(0);
say $r->rand();
say $r->rand();
say $r->rand();
# Output:
# 0.548813502304256
# 0.592844616388902
# 0.715189364971593
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment