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

조회 수: 3 (최근 30일)
JangHo Cho
JangHo Cho 2015년 8월 4일
댓글: JangHo Cho 2015년 8월 5일
Hello,
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?

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 8월 4일
Use rng function
for k=1:10
rng(k)
randn(1,10)
end
  댓글 수: 1
JangHo Cho
JangHo Cho 2015년 8월 5일
Thank you Azzi Abdelmalek. I understood by your code. it must be added to my function.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mike Hosea
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
JangHo Cho
JangHo Cho 2015년 8월 5일
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. :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by