MATLAB Answers


randn() function generates the same sequence whenever I called it.

Asked by JangHo Cho on 4 Aug 2015
Latest activity Commented on by JangHo Cho on 5 Aug 2015
I used MATLAB coder for converting .m files to C files.
All the things are O.K. except randn() function. The problem is that auto-generated randn() function generates the same sequence of numbers all the time. So I changed it by using 'srand(time(NULL))'; and 'rand/(double)RAND_MAX';
Are there any settings for MATLAB coder to generated a different version of randn() function?


Sign in to comment.

2 Answers

回答者: Azzi Abdelmalek
2015 年 8 月 4 日

Use rng function
for k=1:10

  1 Comment

Thank you Azzi Abdelmalek. I understood by your code. it must be added to my function.

Sign in to comment.

回答者: Mike Hosea
2015 年 8 月 4 日

Every time you start MATLAB it will deliver the same sequence if you don't provide your own seed first. Your generated code must match this behavior. Consequently, if that isn't what you want, you must use the rng function to seed the generator. There is an example here on MATLAB Answers somewhere that illustrates using the C time() function to with rng to seed the generator.

  1 Comment

Thank you for your kindness, Mike Hosea. I will search for the article you mentioned. Though I feel more comfortable to write directly in C code, I should search for it because in many cases we don't want to modify the auto-generated code lines. I thank both of you but there was only one chance to click the 'Accept this Answer' button. sorry. :)

Sign in to comment.