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:
How to generate different random numbers

Subject: How to generate different random numbers

From: yunzhi cheng

Date: 29 May, 2007 10:52:28

Message: 1 of 16

I have to generate three diffrent random integars between 1 and n.

If I use
round(rand(1,3)*(n-1)+1)

maybe some of the three number are the same.

I wonder if thre is better way except generating them one by one?

thanks.

Subject: How to generate different random numbers

From: us

Date: 29 May, 2007 11:00:47

Message: 2 of 16

yunzhi cheng:
<SNIP a few rand ints evergreen...

one of the many solutions

     n=10;
     m=3;
     r=randperm(n);
     r=r(1:m)

us

Subject: How to generate different random numbers

From: ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford)

Date: 29 May, 2007 19:14:03

Message: 3 of 16

In article <ef58601.-1@webcrossing.raydaftYaTP>, "yunzhi cheng"
<sjtu_yh@yahoo.com> wrote:

> I have to generate three diffrent random integars between 1 and n.
>
> If I use
> round(rand(1,3)*(n-1)+1)
>
> maybe some of the three number are the same.
>
> I wonder if thre is better way except generating them one by one?
>
> thanks.

In article <ef58601.0@webcrossing.raydaftYaTP>, us <us@neurol.unizh.ch> wrote:

> yunzhi cheng:
> <SNIP a few rand ints evergreen...
>
> one of the many solutions
>
> n=10;
> m=3;
> r=randperm(n);
> r=r(1:m)
>
> us
-----------------
  With only three distinct integers to generate, you may be interested in
the following alternative method if execution time is an issue, Yunzhi.
It calls on 'rand' just three times instead of the n times required by
'randperm', and therefore ought be faster for large values of n, even
though it is less elegant-looking and uses more lines of code. For n =
50, it is distinctly faster on my own machine.

 i1 = ceil(n*rand);
 i2 = ceil((n-1)*rand);
 i3 = ceil((n-2)*rand);
 if i2 < i1
  i3 = i3 + (i3>=i2) + (i3>=i1-1);
 else
  i2 = i2 + 1;
  i3 = i3 + (i3>=i1) + (i3>=i2-1);
 end

The quantities, i1, i2, and i3 are the three desired integers. They are
always distinct and each may range from 1 to n. The property of being
statistically uniformly distributed among all n*(n-1)*(n-2) possible
sequences of distinct integers from 1 to n is retained.

  Note: This works with my ancient version (4a), but you might need to
convert the above logicals to doubles to make it work, that is,
double(i3>=i2), etc.

Roger Stafford

Subject: How to generate different random numbers

From: Paul Mennen

Date: 30 May, 2007 03:57:08

Message: 4 of 16

> I have to generate three diffrent random integars between
> 1 and n.

Here is yet another solution, not all that
different that Roger's idea:

r = ceil(rand(1,3).*(n-2:n)); % get 3 random integers
r(2) = mod(sum(r(2:3))-1,n)+1; % r2 & r3 are different
v = 1:n;
v(r(2:3)) = 0;
v = find(v>0); % a list of numbers not including r2 & r3
r(1) = v(r(1)); % pick r1 to be one of those at random

~Paul

Subject: How to generate different random numbers

From: Peter Bone

Date: 30 May, 2007 04:36:38

Message: 5 of 16

Paul Mennen wrote:
>
>
>> I have to generate three diffrent random integars between
>> 1 and n.
>
> Here is yet another solution, not all that
> different that Roger's idea:
>
> r = ceil(rand(1,3).*(n-2:n)); % get 3 random integers
> r(2) = mod(sum(r(2:3))-1,n)+1; % r2 & r3 are different
> v = 1:n;
> v(r(2:3)) = 0;
> v = find(v>0); % a list of numbers not including r2 & r3
> r(1) = v(r(1)); % pick r1 to be one of those at random
>
> ~Paul
  
Here's a general method to return m numbers with only m calls to
rand. It uses a loop though. Vectorize?

function r = randperm2(n, m)
p = 1 : n;
len = n;
r = zeros(1,m);
for a = 1 : m
    i = ceil(rand * len);
    r(a) = p(i);
    p(i) = p(len);
    len = len - 1;
end

Peter Bone

Subject: How to generate different random numbers

From: Cristina

Date: 1 Jun, 2007 14:08:25

Message: 6 of 16

This doesn't address the exact desire for the random number
generation but you could start here.

Generate M random integers from 0 to N-1 via the following:
X=mod(randperm(M),N);

Then X contains a vector of random values of length M from 0 to N-1.
They won't be all unique. For that I'd mix in the use of the UNIQUE
command from matlab. I hope this helps. A histogram of this result
shows a nice uniform distribution. To get a distribution from L to N
just do new_X=X+L ;) for any value of L.

Anyone who can add to this please feel free to comment,
Cristina

 Peter Bone wrote:
>
>
> Paul Mennen wrote:
>>
>>
>>> I have to generate three diffrent random integars between
>>> 1 and n.
>>
>> Here is yet another solution, not all that
>> different that Roger's idea:
>>
>> r = ceil(rand(1,3).*(n-2:n)); % get 3 random integers
>> r(2) = mod(sum(r(2:3))-1,n)+1; % r2 & r3 are different
>> v = 1:n;
>> v(r(2:3)) = 0;
>> v = find(v>0); % a list of numbers not including r2 & r3
>> r(1) = v(r(1)); % pick r1 to be one of those at random
>>
>> ~Paul
>
> Here's a general method to return m numbers with only m calls to
> rand. It uses a loop though. Vectorize?
>
> function r = randperm2(n, m)
> p = 1 : n;
> len = n;
> r = zeros(1,m);
> for a = 1 : m
> i = ceil(rand * len);
> r(a) = p(i);
> p(i) = p(len);
> len = len - 1;
> end
>
> Peter Bone

Subject: How to generate different random numbers

From: Jos

Date: 1 Jun, 2007 14:48:13

Message: 7 of 16

Cristina wrote:
>
>
> This doesn't address the exact desire for the random number
> generation but you could start here.
>
> Generate M random integers from 0 to N-1 via the following:
> X=mod(randperm(M),N);
>
> Then X contains a vector of random values of length M from 0 to
> N-1.
> They won't be all unique. For that I'd mix in the use of the
> UNIQUE
> command from matlab. I hope this helps. A histogram of this result
> shows a nice uniform distribution. To get a distribution from L to
> N
> just do new_X=X+L ;) for any value of L.
>
> Anyone who can add to this please feel free to comment,
> Cristina
>
...

But
R = randperm(M) ;
R = R(1:N) ;

will already produce uniformly N unique random numbers chosen from
the range of 1-M, without using MOD and UNIQUE.

and of course, one cannot draw N unique numbers between 1 and M if N
> M ...

Jos

Subject: How to generate different random numbers

From: Joseph

Date: 1 Jun, 2007 15:17:08

Message: 8 of 16

It's nice to see someone from Brownsville posting on this. However,
I'm not sure what mod would buy you here.

I think the original question was for a method that would be robust
enough to ensure there would not be a repetition no matter what the
random number generator provides. If that's the case:

a = [1:M];
select = unique(round(rand(margins,1)*M+1));
finalvalues = a(select(1:N));

where margins is some number greater than M. Basically, M-N should
represent your worst case repetition rate.

 Cristina wrote:
>
>
> This doesn't address the exact desire for the random number
> generation but you could start here.
>
> Generate M random integers from 0 to N-1 via the following:
> X=mod(randperm(M),N);
>
> Then X contains a vector of random values of length M from 0 to
> N-1.
> They won't be all unique. For that I'd mix in the use of the
> UNIQUE
> command from matlab. I hope this helps. A histogram of this result
> shows a nice uniform distribution. To get a distribution from L to
> N
> just do new_X=X+L ;) for any value of L.
>
> Anyone who can add to this please feel free to comment,
> Cristina
>
> Peter Bone wrote:
>>
>>
>> Paul Mennen wrote:
>>>
>>>
>>>> I have to generate three diffrent random integars
between
>>>> 1 and n.
>>>
>>> Here is yet another solution, not all that
>>> different that Roger's idea:
>>>
>>> r = ceil(rand(1,3).*(n-2:n)); % get 3 random integers
>>> r(2) = mod(sum(r(2:3))-1,n)+1; % r2 & r3 are different
>>> v = 1:n;
>>> v(r(2:3)) = 0;
>>> v = find(v>0); % a list of numbers not including r2 &
r3
>>> r(1) = v(r(1)); % pick r1 to be one of those at random
>>>
>>> ~Paul
>>
>> Here's a general method to return m numbers with only m calls
to
>> rand. It uses a loop though. Vectorize?
>>
>> function r = randperm2(n, m)
>> p = 1 : n;
>> len = n;
>> r = zeros(1,m);
>> for a = 1 : m
>> i = ceil(rand * len);
>> r(a) = p(i);
>> p(i) = p(len);
>> len = len - 1;
>> end
>>
>> Peter Bone

Subject: How to generate different random numbers

From: Cristina

Date: 2 Jun, 2007 16:52:15

Message: 9 of 16

Hi Joseph,

Does the use of UNIQUE sort the results. This breaks the random
nature that you want to preserve. In my post I figured UNIQUE could
play a role. Matlab loves vectorized operations. I thought it
sorted the "unique"ed entries. If so that seems oddly not random ;).
Oh, to clarify.
 The use of randperm() gives a list like.
     5 6 1 4 2 3
of a randomized integer list containing M values from 1 -> M. If
I use MOD I can reduce this list to M entries with values from 1
-> L
If I want the random value between 0->1 using MOD(randperm(6),2)
gives
    1 0 1 0 0 1

Infinite choices = Infinite possibilities
;)

See ya,
Cristina

Joseph wrote:
>
>
> It's nice to see someone from Brownsville posting on this.
> However,
> I'm not sure what mod would buy you here.
>
> I think the original question was for a method that would be robust
> enough to ensure there would not be a repetition no matter what the
> random number generator provides. If that's the case:
>
> a = [1:M];
> select = unique(round(rand(margins,1)*M+1));
> finalvalues = a(select(1:N));
>
> where margins is some number greater than M. Basically, M-N should
> represent your worst case repetition rate.
>
> Cristina wrote:
>>
>>
>> This doesn't address the exact desire for the random number
>> generation but you could start here.
>>
>> Generate M random integers from 0 to N-1 via the following:
>> X=mod(randperm(M),N);
>>
>> Then X contains a vector of random values of length M from 0 to
>> N-1.
>> They won't be all unique. For that I'd mix in the use of the
>> UNIQUE
>> command from matlab. I hope this helps. A histogram of this
> result
>> shows a nice uniform distribution. To get a distribution from L
> to
>> N
>> just do new_X=X+L ;) for any value of L.
>>
>> Anyone who can add to this please feel free to comment,
>> Cristina
>>
>> Peter Bone wrote:
>>>
>>>
>>> Paul Mennen wrote:
>>>>
>>>>
>>>>> I have to generate three diffrent random integars
> between
>>>>> 1 and n.
>>>>
>>>> Here is yet another solution, not all that
>>>> different that Roger's idea:
>>>>
>>>> r = ceil(rand(1,3).*(n-2:n)); % get 3 random integers
>>>> r(2) = mod(sum(r(2:3))-1,n)+1; % r2 & r3 are different
>>>> v = 1:n;
>>>> v(r(2:3)) = 0;
>>>> v = find(v>0); % a list of numbers not including
r2
> &
> r3
>>>> r(1) = v(r(1)); % pick r1 to be one of those at random
>>>>
>>>> ~Paul
>>>
>>> Here's a general method to return m numbers with only m
calls
> to
>>> rand. It uses a loop though. Vectorize?
>>>
>>> function r = randperm2(n, m)
>>> p = 1 : n;
>>> len = n;
>>> r = zeros(1,m);
>>> for a = 1 : m
>>> i = ceil(rand * len);
>>> r(a) = p(i);
>>> p(i) = p(len);
>>> len = len - 1;
>>> end
>>>
>>> Peter Bone

Subject: How to generate different random numbers

From: Joseph

Date: 3 Jun, 2007 11:19:18

Message: 10 of 16

Cristina,
unfortunately, I did not check up on the function randperm until
after I posted. Sigh. Ooops. It was one of those things where you
hit 'post my reply' and a second later you regret it. It works well
for the poster's requirements

Anyways, the poster just said they wanted to have a random set of
numbers that never repeated. So, yes unique will sort the random
numbers generated, but that didn't seem to matter.

The reason I was confused about the mod was because you end up with
repeating values, right? So, that fails the poster's requirements.

What are you working on down there?

 Cristina wrote:
>
>
> Hi Joseph,
>
> Does the use of UNIQUE sort the results. This breaks the random
> nature that you want to preserve. In my post I figured UNIQUE
> could
> play a role. Matlab loves vectorized operations. I thought it
> sorted the "unique"ed entries. If so that seems oddly not random
> ;).
> Oh, to clarify.
> The use of randperm() gives a list like.
> 5 6 1 4 2 3
> of a randomized integer list containing M values from 1 -> M.
> If
> I use MOD I can reduce this list to M entries with values from 1
> -> L
> If I want the random value between 0->1 using MOD(randperm(6),2)
> gives
> 1 0 1 0 0 1
>
> Infinite choices = Infinite possibilities
> ;)
>
> See ya,
> Cristina
>
> Joseph wrote:
>>
>>
>> It's nice to see someone from Brownsville posting on this.
>> However,
>> I'm not sure what mod would buy you here.
>>
>> I think the original question was for a method that would be
> robust
>> enough to ensure there would not be a repetition no matter what
> the
>> random number generator provides. If that's the case:
>>
>> a = [1:M];
>> select = unique(round(rand(margins,1)*M+1));
>> finalvalues = a(select(1:N));
>>
>> where margins is some number greater than M. Basically, M-N
> should
>> represent your worst case repetition rate.
>>
>> Cristina wrote:
>>>
>>>
>>> This doesn't address the exact desire for the random number
>>> generation but you could start here.
>>>
>>> Generate M random integers from 0 to N-1 via the following:
>>> X=mod(randperm(M),N);
>>>
>>> Then X contains a vector of random values of length M from
0
> to
>>> N-1.
>>> They won't be all unique. For that I'd mix in the use of
the
>>> UNIQUE
>>> command from matlab. I hope this helps. A histogram of
this
>> result
>>> shows a nice uniform distribution. To get a distribution
from
> L
>> to
>>> N
>>> just do new_X=X+L ;) for any value of L.
>>>
>>> Anyone who can add to this please feel free to comment,
>>> Cristina
>>>
>>> Peter Bone wrote:
>>>>
>>>>
>>>> Paul Mennen wrote:
>>>>>
>>>>>
>>>>>> I have to generate three diffrent random
integars
>> between
>>>>>> 1 and n.
>>>>>
>>>>> Here is yet another solution, not all that
>>>>> different that Roger's idea:
>>>>>
>>>>> r = ceil(rand(1,3).*(n-2:n)); % get 3 random
integers
>>>>> r(2) = mod(sum(r(2:3))-1,n)+1; % r2 & r3 are
> different
>>>>> v = 1:n;
>>>>> v(r(2:3)) = 0;
>>>>> v = find(v>0); % a list of numbers not
including
> r2
>> &
>> r3
>>>>> r(1) = v(r(1)); % pick r1 to be one of those at
> random
>>>>>
>>>>> ~Paul
>>>>
>>>> Here's a general method to return m numbers with only m
> calls
>> to
>>>> rand. It uses a loop though. Vectorize?
>>>>
>>>> function r = randperm2(n, m)
>>>> p = 1 : n;
>>>> len = n;
>>>> r = zeros(1,m);
>>>> for a = 1 : m
>>>> i = ceil(rand * len);
>>>> r(a) = p(i);
>>>> p(i) = p(len);
>>>> len = len - 1;
>>>> end
>>>>
>>>> Peter Bone

Subject: How to generate different random numbers

From: Mohammad Rawashdeh

Date: 24 Jan, 2010 23:02:02

Message: 11 of 16

"yunzhi cheng" <sjtu_yh@yahoo.com> wrote in message <ef58601.-1@webcrossing.raydaftYaTP>...
> I have to generate three diffrent random integars between 1 and n.
>
> If I use
> round(rand(1,3)*(n-1)+1)
>
> maybe some of the three number are the same.
>
> I wonder if thre is better way except generating them one by one?
>
> thanks.

Matlab code:

******************************************
% This code simulates picking 3 different items randomly without
% replacement from a bag with n items
% a vector initialized with integer values (1:n) will represent our bag

% the bag: a copy of your items vector
bag = 1:n;

for pick = 1:3
   % find the item index
   item = randi([1 length(bag)], 1);

   % you've got your item here which is bag(item)
   % do whatever you want with it

   % we have to remove the item from the bag; not to be selected again
   bag(item) = [];
end

******************************************
Note: The frequency histogram of the selected items
resembles a uniform distribution.

--Rawashdeh, M.

Subject: How to generate different random numbers

From: Rodrigo

Date: 19 Mar, 2012 11:18:10

Message: 12 of 16

The 'randperm' solution worked like a charm... Thanks!

Rodrigo

ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford) wrote in message <ellieandrogerxyzzy-2905071214030001@dialup-4.232.0.71.dial1.losangeles1.level3.net>...
> In article <ef58601.-1@webcrossing.raydaftYaTP>, "yunzhi cheng"
> <sjtu_yh@yahoo.com> wrote:
>
> > I have to generate three diffrent random integars between 1 and n.
> >
> > If I use
> > round(rand(1,3)*(n-1)+1)
> >
> > maybe some of the three number are the same.
> >
> > I wonder if thre is better way except generating them one by one?
> >
> > thanks.
>
> In article <ef58601.0@webcrossing.raydaftYaTP>, us <us@neurol.unizh.ch> wrote:
>
> > yunzhi cheng:
> > <SNIP a few rand ints evergreen...
> >
> > one of the many solutions
> >
> > n=10;
> > m=3;
> > r=randperm(n);
> > r=r(1:m)
> >
> > us
> -----------------
> With only three distinct integers to generate, you may be interested in
> the following alternative method if execution time is an issue, Yunzhi.
> It calls on 'rand' just three times instead of the n times required by
> 'randperm', and therefore ought be faster for large values of n, even
> though it is less elegant-looking and uses more lines of code. For n =
> 50, it is distinctly faster on my own machine.
>
> i1 = ceil(n*rand);
> i2 = ceil((n-1)*rand);
> i3 = ceil((n-2)*rand);
> if i2 < i1
> i3 = i3 + (i3>=i2) + (i3>=i1-1);
> else
> i2 = i2 + 1;
> i3 = i3 + (i3>=i1) + (i3>=i2-1);
> end
>
> The quantities, i1, i2, and i3 are the three desired integers. They are
> always distinct and each may range from 1 to n. The property of being
> statistically uniformly distributed among all n*(n-1)*(n-2) possible
> sequences of distinct integers from 1 to n is retained.
>
> Note: This works with my ancient version (4a), but you might need to
> convert the above logicals to doubles to make it work, that is,
> double(i3>=i2), etc.
>
> Roger Stafford

Subject: How to generate different random numbers

From: Peter Perkins

Date: 19 Mar, 2012 15:32:07

Message: 13 of 16

I can't tell how old the original part of this thread is, but since
R2011a, the randperm function has a syntax that simplifies this a bit
and addressees Roger's concern:

 >> randperm(10,3)
ans =
      9 5 4



On 3/19/2012 7:18 AM, Rodrigo wrote:
> The 'randperm' solution worked like a charm... Thanks!
>
> Rodrigo
>
> ellieandrogerxyzzy@mindspring.com.invalid (Roger Stafford) wrote in
> message
> <ellieandrogerxyzzy-2905071214030001@dialup-4.232.0.71.dial1.losangeles1.level3.net>...
>
>> In article <ef58601.-1@webcrossing.raydaftYaTP>, "yunzhi cheng"
>> <sjtu_yh@yahoo.com> wrote:
>>
>> > I have to generate three diffrent random integars between 1 and n.
>> > > If I use
>> > round(rand(1,3)*(n-1)+1)
>> > > maybe some of the three number are the same.
>> > > I wonder if thre is better way except generating them one by one?
>> > > thanks.
>>
>> In article <ef58601.0@webcrossing.raydaftYaTP>, us
>> <us@neurol.unizh.ch> wrote:
>>
>> > yunzhi cheng:
>> > <SNIP a few rand ints evergreen...
>> > > one of the many solutions
>> > > n=10;
>> > m=3;
>> > r=randperm(n);
>> > r=r(1:m)
>> > > us
>> -----------------
>> With only three distinct integers to generate, you may be interested in
>> the following alternative method if execution time is an issue,
>> Yunzhi. It calls on 'rand' just three times instead of the n times
>> required by
>> 'randperm', and therefore ought be faster for large values of n, even
>> though it is less elegant-looking and uses more lines of code. For n =
>> 50, it is distinctly faster on my own machine.
>>
>> i1 = ceil(n*rand);
>> i2 = ceil((n-1)*rand);
>> i3 = ceil((n-2)*rand);
>> if i2 < i1
>> i3 = i3 + (i3>=i2) + (i3>=i1-1);
>> else
>> i2 = i2 + 1;
>> i3 = i3 + (i3>=i1) + (i3>=i2-1);
>> end
>>
>> The quantities, i1, i2, and i3 are the three desired integers. They are
>> always distinct and each may range from 1 to n. The property of being
>> statistically uniformly distributed among all n*(n-1)*(n-2) possible
>> sequences of distinct integers from 1 to n is retained.
>>
>> Note: This works with my ancient version (4a), but you might need to
>> convert the above logicals to doubles to make it work, that is,
>> double(i3>=i2), etc.
>>
>> Roger Stafford

Subject: How to generate different random numbers

From: Roger Stafford

Date: 19 Mar, 2012 17:06:11

Message: 14 of 16

Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message <jk7jhn$907$1@newscl01ah.mathworks.com>...
> I can't tell how old the original part of this thread is, but since
> R2011a, the randperm function has a syntax that simplifies this a bit
> and addressees Roger's concern:
> >> randperm(10,3)
- - - - - - - - -
  According to my records the original post in this thread was dated 29 May 2007 and my reply was the same day. (It seems like a life time ago now.)

  I assume from your statement, Peter, that the new R2011a randperm(10,3) only calls on the random number generator three times. Am I correct?

Roger Stafford

Subject: How to generate different random numbers

From: Peter Perkins

Date: 19 Mar, 2012 19:50:24

Message: 15 of 16

On 3/19/2012 1:06 PM, Roger Stafford wrote:
> Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message
> <jk7jhn$907$1@newscl01ah.mathworks.com>...
>> I can't tell how old the original part of this thread is, but since
>> R2011a, the randperm function has a syntax that simplifies this a bit
>> and addressees Roger's concern:
>> >> randperm(10,3)
> - - - - - - - - -
> According to my records the original post in this thread was dated 29
> May 2007 and my reply was the same day. (It seems like a life time ago
> now.)
>
> I assume from your statement, Peter, that the new R2011a randperm(10,3)
> only calls on the random number generator three times. Am I correct?
>
> Roger Stafford

That's right, and so even things like

 >> randperm(2^32,3)
ans =
          1196140743 2348838240 4112460542

are fast.

Subject: How to generate different random numbers

From: Khalid

Date: 19 May, 2014 01:32:07

Message: 16 of 16

nice... its working...

Tags for 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