简体   繁体   中英

Seeding F# random generator to same state as Matlab

In trying to port over some Matlab code to F#, I'm trying to make sure the translations are accurate. As of now, there are cases where I'm not completely sure whether there are mistakes. Since a lot of the code is statistical in nature, it would be convenient to be able to seed the F# generators to the same state as Matlab's. It would also help with triangulating the exact equations that are wrong. Wanted to ask before I started dumping Matlab generated random numbers to csv files and solving this issue in a manual way.

This is not a definitive answer as probably implementing your own random number generator in matlab and F# should yield the most reliable results. You are also bound to bump into issues of thread safety in .NET, and the shapes of matrices in matlab. For example

In matlab:

 rng(200,'twister')
 rand(1,5)

ans =

 0.9476 0.2265 0.5944 0.4283 0.7641 

In F#:

open MathNet.Numerics.Random

let random1b = MersenneTwister(200)
random1b.NextDoubles(5)

val it : float [] = [|0.9476322592; 0.4941436297; 0.2265474238; 0.1485590497; 0.5944201448|]

The 1st, 3rd, and 5th random numbers do match.

Now it's possible you can replicate this somehow by playing around with different versions and/or F# and matlab array dimensions.

The MathNet Random Docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM