Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: random numbers
Date: Wed, 17 Jun 2009 09:34:38 -0400
Organization: The MathWorks, Inc.
Lines: 58
Message-ID: <h1arcd$l8h$1@fred.mathworks.com>
References: <h1abg5$t3k$1@fred.mathworks.com> <h1aide$bek$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1245245645 21777 144.212.105.187 (17 Jun 2009 13:34:05 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 17 Jun 2009 13:34:05 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
Xref: news.mathworks.com comp.soft-sys.matlab:548279



"Ulf Graewe" <graewe@icbm.de.skip.this> wrote in message 
news:h1aide$bek$1@fred.mathworks.com...
> you can something like this
>
> function [Msgsize] = sizetext(number,key)
>
> if ~isemptey(key)
>    rand('state',key)
> endif
> number = 300 * 15;
> Msgsize= round(rand(floor(number),1));
> Msgsize= randintrlv(Msgsize);
>
> by using the rand('state',number) command, you can set the generator to a 
> certain state and thus get the same sequence of random numbers.
>
> simply try in the command line
>
> rand('state',1000),rand,rand
>
> rand('state',1000),rand,rand('state',1000), rand
>
> and see the difference!
>
> doc rand gives more information

Since the introduction of the RandStream random number generator 
infrastructure, we recommend using RandStream methods and properties to 
control the random number generators, rather than the rand(keyword, ...) 
syntax.  Doing so allows you to isolate your own random number generator 
streams, so that your random number generation will not affect others that 
are using RandStream generators and their random number generation will not 
affect yours.  In addition, if you have RandStream available, you will also 
have the RANDI function/method available, which can directly generate 
integer values.  This means you won't need to do ROUND/CEIL/FLOOR/FIX of a 
scaled call to RAND.


function msgsize = sizetext(number, key)
mygenerator = RandStream.create('mt19937ar', 'Seed', key);
numElements = 300*15;
% Generate a numElements-by-1 matrix of integers between 0 and 1 inclusive
% using the generator object I created above.
% mt19937ar is the Mersenne Twister algorithm.  For a list of other 
algorithms
% that are available, see:
% 
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/randstream.list.html
msgsize = randi(mygenerator, [0 1], numElements, 1)
msgsize = randintrlv(msgsize);


-- 
Steve Lord
slord@mathworks.com