I am trying to port a MATLAB code into C++ and found the usage of rand in MATLAB.

Is MATLAB's rand function and C++ rand() function same? If not, is there any inbuilt function for MATLAB's rand in C++ or OpenCV?


closed as off-topic by Paul R, Matt L., Deve, jojek, Dilip Sarwate Jun 14 '14 at 23:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "General programming questions are off-topic here, but can be asked on Stack Overflow." – Paul R, Matt L., Deve, jojek, Dilip Sarwate
If this question can be reworded to fit the rules in the help center, please edit the question.


It is a programming question, not a signal processing question - keep that in mind, and next time use the StackOverflow.

  • MATLAB rand function returns uniformly distributed variable on interval $[0,1]$.
  • C++ rand function returns integer between 0 and RAND_MAX, additionally it is not very 'random' function. You would also have to normalize its output. For more info please refer here: C++ rand() between 0 and 1. Although I think that you should use the uniform_int_distribution function, providing you can take the advantage of C++11 standard.

I believe that you should use OpenCV after all, instead of playing with new standards and some tricky functions. It contains the RNG class that you can easily use for generating of uniformly distributed sequence. Here is the example:

RNG rng;
float r_num = rng.uniform(0, 1);

Not the answer you're looking for? Browse other questions tagged or ask your own question.