Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How to generate a random data?

Subject: How to generate a random data?

From: Rui Yang

Date: 27 Jul, 2007 00:26:54

Message: 1 of 4

I want to generate a random sequence. For example(1,2,3,4).The probability distributing of each element is (20%, 20%, 20%, 40%). How to realize it in Matlab?

Subject: How to generate a random data?

From: Yang Zhang

Date: 27 Jul, 2007 05:08:01

Message: 2 of 4

"Rui Yang" <rui.yang@mathworks.com> wrote in message <ef5ede4.-1@webcrossing.raydaftYaTP>...
> I want to generate a random sequence. For example(1,2,3,4).The probability distributing of each element is (20%, 20%, 20%, 40%). How to realize it in Matlab?



===============
Hi,

A simple but not wise way is:

x = rand(1,1000);
y = 4*ones(1,1000);
y(find(x<-0.6)) = 1;
y(find(x>-0.6 || x < -0.2)) = 2;
y(find(x>-0.2 || x < 0.2)) = 3;

HTH

Yang

Subject: How to generate a random data?

From: Rui Yang

Date: 27 Jul, 2007 05:24:09

Message: 3 of 4

"Yang Zhang" <zhyang99@hotmail.com> wrote in message <f8bujg$6n6$1@fred.mathworks.com>...
> "Rui Yang" <rui.yang@mathworks.com> wrote in message <ef5ede4.-1@webcrossing.raydaftYaTP>...
> > I want to generate a random sequence. For example(1,2,3,4).The probability distributing of each element is (20%, 20%, 20%, 40%). How to realize it in Matlab?
>
>
>
> ===============
> Hi,
>
> A simple but not wise way is:
>
> x = rand(1,1000);
> y = 4*ones(1,1000);
> y(find(x<-0.6)) = 1;
> y(find(x>-0.6 || x < -0.2)) = 2;
> y(find(x>-0.2 || x < 0.2)) = 3;
>
> HTH
>
> Yang

Thanks, It is a good way to get it.

Subject: How to generate a random data?

From: Markus Buehren

Date: 27 Jul, 2007 06:58:52

Message: 4 of 4

It think this function is faster:

function states = randomstate(probs, M, N)
%RANDOMSTATE Randomly choose a state from given discrete probabilites.
% X = RANDOMSTATE(PROBS) gives a random state from the discrete
% probability distribuation given in vector PROBS.
%
% X = RANDOMSTATE(PROBS, M, N) returns an N-by-M-matrix of random states.
%
% For example, RANDOMSTATE([0.1 0.5 0.4]) is 1 with a probability of
% 0.1, 2 with a probability of 0.5 and 3 with a probability of 0.4
%
% Note: The probability vector may contain weight values that are not
% normalized to one.
%
% Markus Buehren

nOfStates = length(probs);
% sumProbs = sum(probs);

if nargin == 1
M = 1;
N = 1;
elseif nargin == 2
N = M;
end

% Find states
if M == 1 && N == 1

sumProbs = sum(probs);
number = rand(1) * sumProbs;
threshold = 0;
for k = 1:nOfStates
threshold = threshold + probs(k);
if number <= threshold
states = k;
return
end
end
states = nOfStates;

% this version is slower:
%index = cumsum(probs);
%states = find(rand(1) * index(end) < index, 1);

else

sumProbs = sum(probs);
numbers = rand(M,N) * sumProbs;
states = ones(M,N);
threshold = 0;
for k = 1:nOfStates-1
threshold = threshold + probs(k);
states(numbers >= threshold) = k+1;
end

end

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
random Ned Gulley 27 Jul, 2007 10:54:33
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics