필터 지우기
필터 지우기

can the value of rand('seed') be saved

조회 수: 4 (최근 30일)
Elysi Cochin
Elysi Cochin 2018년 3월 19일
댓글: Steven Lord 2018년 3월 19일
I set random seed value as 0
rand('seed',0);
then i do genetic algorithm using ga
Is there any way i can save the value of rand('seed',0)
so that i can use it without calling rand('seed',0)
Just load it and call function ga
  댓글 수: 3
Elysi Cochin
Elysi Cochin 2018년 3월 19일
yes sir, thats what i want, "restore the state of the random generator so that any time you use your code it runs exactly the same way in any version of Matlab" Is that possible?
Walter Roberson
Walter Roberson 2018년 3월 19일
Yes: you can have your code call rand('seed',0) . Or, for any MATLAB version since R2011a, better is to call rng(0, 'v4')

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

채택된 답변

Walter Roberson
Walter Roberson 2018년 3월 19일
"Is there any way i can save the value of rand('seed',0) so that i can use it without calling rand('seed',0) Just load it and call function ga"
Yes. You can create a figure and give it a CreateFcn which executes that command. You can make it 'Visible', 'off' so it does not occupy anything on the screen.
Then save the handle of the figure as part of the .mat file .
When you load the .mat file and so load the figure that is contained inside it, the CreateFcn of the figure will be executed; you would have given it code that sets the random seed for you, so the random seed will be changed.
I do not at all recommend this approach: I would suggest that you instead provide a small function or script that loads your data and sets the random seed, and possibly which also starts ga executing.
Note: rand('seed',value) is considered obsolete. You should be using
rng(0, 'v4')
instead.
  댓글 수: 1
Steven Lord
Steven Lord 2018년 3월 19일
So rng(0, 'v4') is the correct way to enable the v4 generator IF you need to exactly reproduce random numbers from a very old installation of MATLAB. [There are people reading Answers today who hadn't been born when MATLAB 5.0 was released in December 1996 with the then-new v5 random number generator.]
If you just want reproducible numbers, I recommend rng(n, 'twister') for some fixed value of n instead.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by