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:
ensuring unique integers with rand

Subject: ensuring unique integers with rand

From: Akim

Date: 14 Dec, 2009 04:53:04

Message: 1 of 4

Dear all,

Is there an elegant way to produce r=ceil(rand(1,500)*20000) with no repeating numbers (i.e. numel(unique(r)) will always equal 500)?

I think randperm is probably a bit an overkill.

[my Matlab does not have randi.m by the way]

Subject: ensuring unique integers with rand

From: Akim

Date: 14 Dec, 2009 05:10:21

Message: 2 of 4

"Akim " <aaa@bbb.ccc> wrote in message <hg4gbg$kb3$1@fred.mathworks.com>...
> Dear all,
>
> Is there an elegant way to produce r=ceil(rand(1,500)*20000) with no repeating numbers (i.e. numel(unique(r)) will always equal 500)?
>
> I think randperm is probably a bit an overkill.
>
> [my Matlab does not have randi.m by the way]

UPDATE: Steven Lord just suggested a rejection method in a different thread [http://www.mathworks.com/matlabcentral/newsreader/view_thread/268317]. But this method is impractical when the sample to pool ratio is suffiently large, as in my case.

E.g.

i=0;
r=ceil(rand(1,500)*20000);
while numel(unique(r)) < 500
    i=i+1;
    r=ceil(rand(1,500)*20000);
end
disp(i)

Subject: ensuring unique integers with rand

From: Steven Lord

Date: 14 Dec, 2009 15:20:17

Message: 3 of 4


"Akim " <aaa@bbb.ccc> wrote in message
news:hg4hbt$p6o$1@fred.mathworks.com...
> "Akim " <aaa@bbb.ccc> wrote in message
> <hg4gbg$kb3$1@fred.mathworks.com>...
>> Dear all,
>>
>> Is there an elegant way to produce r=ceil(rand(1,500)*20000) with no
>> repeating numbers (i.e. numel(unique(r)) will always equal 500)?
>>
>> I think randperm is probably a bit an overkill.
>>
>> [my Matlab does not have randi.m by the way]
>
> UPDATE: Steven Lord just suggested a rejection method in a different
> thread
> [http://www.mathworks.com/matlabcentral/newsreader/view_thread/268317].
> But this method is impractical when the sample to pool ratio is suffiently
> large, as in my case.
>
> E.g.
>
> i=0;
> r=ceil(rand(1,500)*20000);
> while numel(unique(r)) < 500
> i=i+1;
> r=ceil(rand(1,500)*20000);
> end
> disp(i)

Is this block of code _really_ the bottleneck for your function? I tried
running this a dozen or so times; most of the executions took between 0.04
and 0.06 seconds. One took 0.15. I suspect whatever you're actually
_doing_ with these random numbers will probably take more time than this.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: ensuring unique integers with rand

From: Akim

Date: 15 Dec, 2009 05:44:02

Message: 4 of 4

"Steven Lord" <slord@mathworks.com> wrote in message <hg5l0h$bur$1@fred.mathworks.com>...
>
> Is this block of code _really_ the bottleneck for your function? I tried
> running this a dozen or so times; most of the executions took between 0.04
> and 0.06 seconds. One took 0.15. I suspect whatever you're actually
> _doing_ with these random numbers will probably take more time than this.
>

Hi Steven -- this block of code really is the bottleneck. My current fairly crude solution is below, and saves me hours even despite "unique".

u=unique(ceil(rand(1,550)*20000));
r=u(1:500);

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