how do I fix the seed in the trnd function

3 次查看(过去 30 天)
Dear all,
I use matlab 2012 and I want to generate values from a student t distribution. I use the function "trnd". HOw can I fix the see in this function
thanks

采纳的回答

Wayne King
Wayne King 2012-11-10
I'm not sure why you want to do that since you can use rng() without using the legacy mode by removing those calls and using rng() instead.
But if you are insisting, then you have to do something like:
rng(sd,'v5uniform')
for calls to rand()
and
rng(sd,'v5normal')
for randn(), where sd is the seed.
You should enter
>>help rng
at the command line and read the documentation linked from
Updating Your Random Number Generator Syntax.
This is all explained there in detail.
  2 个评论
Sabbas
Sabbas 2012-11-10
编辑:Sabbas 2012-11-10
thank you king.
I tried the following:
initially I had
rand('state',30 );
randn('state',80);
then I did
rng(30,'v5uniform')
rng(80,'v5normal')
but I do not get equivalent results. why?
thanks
Wayne King
Wayne King 2012-11-10
I get the following
rand('state',30)
x = rand(10,1)
%%%now using rng
rng(30,'v5uniform')
y = rand(10,1)
Identical

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2012-11-10
Use the rng() function before you call trnd()
  2 个评论
Sabbas
Sabbas 2012-11-10
编辑:Sabbas 2012-11-10
you mean for example
rng(1) trnd(7)
because I get this erro message
Error using rng (line 96) The current random number generator is the legacy generator. This is because you have executed a command such as rand('state',0), which activates MATLAB's legacy random number behavior. You may not use RNG to reseed the legacy random number generator.
Use rng('default') to reinitialize the random number generator to its startup configuration, or call RNG using a specific generator type, such as rng(seed,'twister').
JUst to mention that in my code I fix the seed for rand and randn before using rng()
thanks

请先登录,再进行评论。


Wayne King
Wayne King 2012-11-10
Don't do that. Don't call rand('state', ) before using rng.
rng seeds the random number generator for you. Remove that from and your code and just use rng() to do the work.
  1 个评论
Sabbas
Sabbas 2012-11-10
ok. Is there a way to keep rand('state',) and fix the random seed for trnd at the same time? because ALL my results are based on rand('state', ) and it is very important for me to keep rand('state', ) and randn('state',) . If I do what you say and remove rand('state,) that would be a disaster.
thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by