Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Randomize between two non contiguous numbers

Subject: Randomize between two non contiguous numbers

From: thesimulator Hassen

Date: 3 Jan, 2011 10:23:04

Message: 1 of 10

Hi guys,

I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.

Subject: Randomize between two non contiguous numbers

From: Greg von Winckel

Date: 3 Jan, 2011 10:55:05

Message: 2 of 10

Do you want a uniform distribution of 2s and 5s?

n=100; % number of randoms
r=2+3*round(rand(n,1)); % random numbers

Is this what you are asking for?

hth,

Greg

"Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> Hi guys,
>
> I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.

Subject: Randomize between two non contiguous numbers

From: Oleg Komarov

Date: 3 Jan, 2011 11:01:07

Message: 3 of 10

"Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> Hi guys,
>
> I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.

% Use two consecutive integers and then convert them
a = randi([0,1],10,1)*5;
a(a == 0) = 2;

Oleg

Subject: Randomize between two non contiguous numbers

From: Oleg Komarov

Date: 3 Jan, 2011 11:05:04

Message: 4 of 10

"Greg von Winckel" wrote in message <ifs9u9$31j$1@fred.mathworks.com>...
> Do you want a uniform distribution of 2s and 5s?
>
> n=100; % number of randoms
> r=2+3*round(rand(n,1)); % random numbers
>
> Is this what you are asking for?
>
> hth,
>
> Greg
>
> "Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> > Hi guys,
> >
> > I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.

Or (as I completely forgot!!), instead of the two lines of code I supplied before:
randi([0,1],10,1)*3+2

Oleg

Subject: Randomize between two non contiguous numbers

From: Tewodros Hassen

Date: 3 Jan, 2011 11:31:04

Message: 5 of 10

Yes, It works for the example I forward in my question. But can we make it moe general? Like not only for two numbers? Or in another word can we generate 100 numbers which are a uniform distribution of any three numbers ?

Thank you
"Greg von Winckel" wrote in message <ifs9u9$31j$1@fred.mathworks.com>...
> Do you want a uniform distribution of 2s and 5s?
>
> n=100; % number of randoms
> r=2+3*round(rand(n,1)); % random numbers
>
> Is this what you are asking for?
>
> hth,
>
> Greg
>
> "Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> > Hi guys,
> >
> > I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.

Subject: Randomize between two non contiguous numbers

From: Oleg Komarov

Date: 3 Jan, 2011 11:42:04

Message: 6 of 10

"Tewodros Hassen" wrote in message <ifsc1o$hio$1@fred.mathworks.com>...
> Yes, It works for the example I forward in my question. But can we make it moe general? Like not only for two numbers? Or in another word can we generate 100 numbers which are a uniform distribution of any three numbers ?
>
> Thank you
> "Greg von Winckel" wrote in message <ifs9u9$31j$1@fred.mathworks.com>...
> > Do you want a uniform distribution of 2s and 5s?
> >
> > n=100; % number of randoms
> > r=2+3*round(rand(n,1)); % random numbers
> >
> > Is this what you are asking for?
> >
> > hth,
> >
> > Greg
> >
> > "Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> > > Hi guys,
> > >
> > > I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.

For any sample size "n" with any set of integers "s":
n = 100;
s = [19, 33, 48];
Out = s(randi([1,numel(s)],n,1));

Oleg

Subject: Randomize between two non contiguous numbers

From: Greg von Winckel

Date: 3 Jan, 2011 11:45:22

Message: 7 of 10

"Tewodros Hassen" wrote in message <ifsc1o$hio$1@fred.mathworks.com>...
> Yes, It works for the example I forward in my question. But can we make it moe general? Like not only for two numbers? Or in another word can we generate 100 numbers which are a uniform distribution of any three numbers ?
>
> Thank you
> "Greg von Winckel" wrote in message <ifs9u9$31j$1@fred.mathworks.com>...
> > Do you want a uniform distribution of 2s and 5s?
> >
> > n=100; % number of randoms
> > r=2+3*round(rand(n,1)); % random numbers
> >
> > Is this what you are asking for?
> >
> > hth,
> >
> > Greg
> >
> > "Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> > > Hi guys,
> > >
> > > I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.


Well as indicated above, you can use

v=randi(3,100,1);

which will give you values of 1,2,3. If you want to replace these by some other values, eg 5,23,37

you can do this

v(v==1)=5;
v(v==2)=23;
v(v==3)=37;

Subject: Randomize between two non contiguous numbers

From: Tewodros Hassen

Date: 3 Jan, 2011 11:53:05

Message: 8 of 10

"Greg von Winckel" wrote in message <ifscsi$b9r$1@fred.mathworks.com>...
> "Tewodros Hassen" wrote in message <ifsc1o$hio$1@fred.mathworks.com>...
> > Yes, It works for the example I forward in my question. But can we make it moe general? Like not only for two numbers? Or in another word can we generate 100 numbers which are a uniform distribution of any three numbers ?
> >
> > Thank you
> > "Greg von Winckel" wrote in message <ifs9u9$31j$1@fred.mathworks.com>...
> > > Do you want a uniform distribution of 2s and 5s?
> > >
> > > n=100; % number of randoms
> > > r=2+3*round(rand(n,1)); % random numbers
> > >
> > > Is this what you are asking for?
> > >
> > > hth,
> > >
> > > Greg
> > >
> > > "Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...

Thank you all. Have got an idea now!
> > > > Hi guys,
> > > >
> > > > I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.
>
>
> Well as indicated above, you can use
>
> v=randi(3,100,1);
>
> which will give you values of 1,2,3. If you want to replace these by some other values, eg 5,23,37
>
> you can do this
>
> v(v==1)=5;
> v(v==2)=23;
> v(v==3)=37;

Subject: Randomize between two non contiguous numbers

From: Jos (10584)

Date: 3 Jan, 2011 12:43:04

Message: 9 of 10

"Greg von Winckel" wrote in message <ifscsi$b9r$1@fred.mathworks.com>...
> "Tewodros Hassen" wrote in message <ifsc1o$hio$1@fred.mathworks.com>...
> > Yes, It works for the example I forward in my question. But can we make it moe general? Like not only for two numbers? Or in another word can we generate 100 numbers which are a uniform distribution of any three numbers ?
> >
> > Thank you
> > "Greg von Winckel" wrote in message <ifs9u9$31j$1@fred.mathworks.com>...
> > > Do you want a uniform distribution of 2s and 5s?
> > >
> > > n=100; % number of randoms
> > > r=2+3*round(rand(n,1)); % random numbers
> > >
> > > Is this what you are asking for?
> > >
> > > hth,
> > >
> > > Greg
> > >
> > > "Tewodros Hassen" wrote in message <ifs828$332$1@fred.mathworks.com>...
> > > > Hi guys,
> > > >
> > > > I want to randomize, say ten numbers, between a set of numbers say 2 and 5. Can any one help me how I can do that? P.S. the ten random numbers can only have values of 2 or 5. not 2 to 5. Thank you.
>
>
> Well as indicated above, you can use
>
> v=randi(3,100,1);
>
> which will give you values of 1,2,3. If you want to replace these by some other values, eg 5,23,37
>
> you can do this
>
> v(v==1)=5;
> v(v==2)=23;
> v(v==3)=37;

The replacing procedure can become quite cumbersome for longer lists ...

Take a look at RANDSAMPLE if you have the Stats TB, or alternatively, use indexing

% data
  ListofValues = [9 -7 1 123]
  Ndraws = 10

% engine
  Nvalues = numel(ListofValues)
  idx = randi(Nvalues, [1 Ndraws])
  Draws = ListofValues(idx)

hth
Jos

Subject: Randomize between two non contiguous numbers

From: Rizki

Date: 4 May, 2013 22:05:09

Message: 10 of 10

This thread really helps my project, thank you so much :D

Tags for this Thread

No tags are associated with this thread.

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.

Contact us