user1698581
user1698581

Reputation: 73

Source code for functions Rand and Randn in Matlab

I'm student from Ariel University in Israel and I'm trying to implement Matlab RAND and RANDN in C# in such way that same input for Matlab and C# (with same seed) , Randn and Rand will give the same result in both languages.

for example:

In Matlab:

rand('seed',123)
disp(rand)

output: 0.0878

In C#:

Console.WriteLine(MyRand(123));

output: 0.0878

I think for implement this kind of functionality, I need to have the source code for RAND and RANDN in Matlab. Does anyone has this code and may share?

Thanks a lot,

Shimon

Upvotes: 1

Views: 2658

Answers (4)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

As your question only mentions obtaining the same results, I would recommend one of the following:

  1. Generate a lot of random numbers, then use them one by one in both programming languages.
  2. Implement your own (simple) random generator in both languages.

Upvotes: 1

sebastian
sebastian

Reputation: 9696

Doing:

>> s = RandStream.getGlobalStream()

s = 


mt19937ar random stream (current global stream)
             Seed: 0
  NormalTransform: Ziggurat

Your given the random-number-generator algorithm and the transformation used to get normal distributed numbers. Both are publicly available algorithms. Google gives you e.g.:

http://www.math.sci.hiroshima-u.ac.jp/~%20m-mat/MT/MT2002/emt19937ar.html and http://www.jstatsoft.org/v05/i08/paper

describing both algorithms including reference / example implemenations.

Upvotes: 3

Daniel
Daniel

Reputation: 36710

Randn is as far as i know MarsenneTwister. To verify this i would first try to use the MarsenneTwister from Apache and check for similar results: http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/random/MersenneTwister.html

If so: Search for any implementation. This algorithm should be documented.

Upvotes: 2

High Performance Mark
High Performance Mark

Reputation: 78334

But seriously, if you type

edit rand.m

into the Matlab command window, and

edit randn.m

I think you will get as much information as the Mathworks publish about those functions. This information points towards the algorithms used and, for rand an implementation too.

Upvotes: 1

Related Questions