DSPRelated.com
Forums

MEX functions and random numbers

Started by gkaf...@yahoo.gr October 9, 2008
Hello all,
I want to write a MEX function to import a part of C code into MATLAB. This piece of code is a normally distributed random number generator. It is controlled by a seed() function which has to be called to initialize the algorithm.
What I am trying to do is to call this seed() function inside the MEX file using a specific number as its arguments (seed(10), for example) in order to get a "controlled" sequence of random numbers. The problem is that wherever I put this seed() function (inside MexFunction, inside the generator algorithm function or anywhere else), the result is the same sequence of numbers each time I call the function. What I want is to get different results for each call of the function (which is the normal case) but after re-setting the seed(), the result sequence must be the same as the previous one. For example:

% start of code
% code
% ....
X = norm_numbers(1, 0, 4) % A vector with 4 normally distributed random numbers
Let's say X = [ 0.4 , 0.1 , -0.33 , 0.27 ].
% code
% ....
Y = norm_numbers(1, 0, 4)
Let's say Y = [ 0.23 , 0.41 , 0.54 , -0.11] .
% ....
% more code
% end of code

After rerunning the code, I want X and Y to be the same as the above ones.
Any help would be appreciated.

G.

P.S: The algorithm that I'm working is the polar form of Box-Muller transform.