Thread Subject: random numbers

Subject: random numbers

From: remi

Date: 17 Jun, 2009 09:03:01

Message: 1 of 3

Hello.... please i need your help with this. I want to generate a random sequence of that value...so i used this [Msgsize= round(rand(floor(number),1))]. After generating that i used a key such that whenever i call the function it gives me exactly that particular random sequence. i know i'm generating randomly but using the key should give me the exact numbers back, because i need the numbers to not change.

Please can someone let me know if i'm on the right track..
many thanks


function [Msgsize] = sizetext(number,key)
number = 300 * 15;
Msgsize= round(rand(floor(number),1));
Msgsize= randintrlv(Msgsize, key);

Subject: random numbers

From: Ulf Graewe

Date: 17 Jun, 2009 11:01:02

Message: 2 of 3

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

Subject: random numbers

From: Steven Lord

Date: 17 Jun, 2009 13:34:38

Message: 3 of 3


"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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
state Ulf Graewe 17 Jun, 2009 07:04:09
rand Ulf Graewe 17 Jun, 2009 07:04:09
random numbers remi 17 Jun, 2009 05:04:04
rssFeed for this Thread

Contact us at files@mathworks.com