how do I fix the seed in the trnd function

2 views (last 30 days)
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

Accepted Answer

Wayne King
Wayne King on 10 Nov 2012
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 Comments
Sabbas
Sabbas on 10 Nov 2012
Edited: Sabbas on 10 Nov 2012
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 on 10 Nov 2012
I get the following
rand('state',30)
x = rand(10,1)
%%%now using rng
rng(30,'v5uniform')
y = rand(10,1)
Identical

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 10 Nov 2012
Use the rng() function before you call trnd()
  2 Comments
Sabbas
Sabbas on 10 Nov 2012
Edited: Sabbas on 10 Nov 2012
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

Sign in to comment.


Wayne King
Wayne King on 10 Nov 2012
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 Comment
Sabbas
Sabbas on 10 Nov 2012
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

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!