pseudo-random number generator rand() in .mex

2 views (last 30 days)
In standard C, the following code generate 5 random numbers.
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
/* Now generate 5 pseudo-random numbers */
int i;
for (i=0; i<5; i++)
{
printf ("rand[%d]= %u\n",
i, rand ());
}
return 0;
}
Each time you run the program, same serial of 5 numbers will be generated since the seed is retested. However, the same code as a mex function does not behave like this. Here is the mex code:
#include <stdio.h>
#include <stdlib.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
/* Now generate 5 pseudo-random numbers */
int i;
for (i=0; i<5; i++)
{
mexPrintf ("rand[%d]= %u\n",
i, rand ());
}
return;
}
Every time this mex function produces different serial of 5 random numbers. Does anybody know why? How the mex function resets the seed number each time? Thanks in advance.
  2 Comments
Elliott Rachlin
Elliott Rachlin on 12 Oct 2018
I notice that in a mex file, the code "rand()" seems to return an integer between 0 and 32767, not the value between 0.0 and 1.0 that I was expecting. Does anybody know why this is (and why the documentation does not seem to mention this)?

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 3 May 2011
When you first run a mex function it loads into memory and stays in memory until it gets cleared. So any variables that are global or persistent in the mex function, like a random number generator seed that the rng internally maintains, will retain the last value between calls. So you get a new set of numbers with each call. If you want to start fresh each time you can either clear the mex function from memory or in the mex code itself reinitialized the rng each time you enter the mex function.

More Answers (1)

James
James on 3 May 2011
Thanks.
If I use the following mex function, seedit_mex, to set the seed each time before calling rand() in another mex functions.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
unsigned int iseed;
/* Simple "srand()" seed: just use "time()" */
iseed = (unsigned int)time(NULL);
srand (iseed);
mexPrintf ("%d\n", iseed);
return;
}
I guess it will work--seedit_mex will reset the global seed and will affect all other mex function calling rand(), until I do:
clear seedit_mex
clear other_mex_function
Am I right?
  1 Comment
James Tursa
James Tursa on 3 May 2011
Yes and no. Using srand (or rand) in one mex routine will affect other mex routines that use rand. However, there is apparently another piece of the library hanging around in memory even after you clear the mex routines, so at least in my testing you will not get a fresh start this way by just doing clear. You must use srand.

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!