Thread Subject: Random numer generation without repetition

Subject: Random numer generation without repetition

From: Kishore

Date: 25 Apr, 2009 08:52:02

Message: 1 of 8

Hi,

I was wondering if it is possible to generate random numbers in the following scenario,

random numbers between 1 to 200, ( i can do it),
but i do not want them to be repeated. for example if i select a sample of 40 elements
it should have random numbers between 1 to 200, but not repeated.

I can use rand function for this, but how can i avoid repetition, by using some modification to this rand () thereby saving some lines of code to check for repetition.



thanks!

Subject: Random numer generation without repetition

From: dpb

Date: 25 Apr, 2009 14:31:20

Message: 2 of 8

Kishore wrote:
> Hi,
>
> I was wondering if it is possible to generate random numbers in the following scenario,
> random numbers between 1 to 200, ( i can do it),
> but i do not want them to be repeated. for example if i select a sample of 40 elements
> it should have random numbers between 1 to 200, but not repeated.
>
> I can use rand function for this, but how can i avoid repetition, by
> using some modification to this rand () thereby saving some lines of
> code to check for repetition....

One way would be with randperm() (which will use rand() for you)


--

Subject: Random numer generation without repetition

From: Matt Fig

Date: 25 Apr, 2009 14:40:19

Message: 3 of 8

Check out randperm.

Subject: Random numer generation without repetition

From: uri

Date: 26 Nov, 2009 07:25:04

Message: 4 of 8

"Matt Fig" <spamanon@yahoo.com> wrote in message <gsv7cj$oj0$1@fred.mathworks.com>...
> Check out randperm.

nice approach but not good enough, I need a small number of elmennts reletively to the maximum sampling size. if I'd use randperm it'll blow my memory

Subject: Random numer generation without repetition

From: TideMan

Date: 26 Nov, 2009 07:40:46

Message: 5 of 8

On Nov 26, 8:25 pm, "uri " <sshango...@hotmail.com> wrote:
> "Matt Fig" <spama...@yahoo.com> wrote in message <gsv7cj$oj...@fred.mathworks.com>...
> > Check out randperm.
>
> nice approach but not good enough, I need a small number of elmennts reletively to the maximum sampling size. if I'd use randperm it'll blow my memory

Huh???
tic;y=randperm(200);toc
Elapsed time is 0.001623 seconds.

Did you read the help correctly?
Or is there something that you're not telling us?

Subject: Random numer generation without repetition

From: uri

Date: 26 Nov, 2009 07:47:03

Message: 6 of 8

TideMan <mulgor@gmail.com> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9871@r24g2000prf.googlegroups.com>...
> On Nov 26, 8:25?pm, "uri " <sshango...@hotmail.com> wrote:
> > "Matt Fig" <spama...@yahoo.com> wrote in message <gsv7cj$oj...@fred.mathworks.com>...
> > > Check out randperm.
> >
> > nice approach but not good enough, I need a small number of elmennts reletively to the maximum sampling size. if I'd use randperm it'll blow my memory
>
> Huh???
> tic;y=randperm(200);toc
> Elapsed time is 0.001623 seconds.
>
> Did you read the help correctly?
> Or is there something that you're not telling us?

let's say I have a vector (file wise) on my harddisk which has 10^8 cells
I"d like to sample m elements out of this vector but using:
gen=randperm(10^8);
x=gen(1:m);
gen is too big
I just need the m elements without generating all the 10^8 numbers

Subject: Random numer generation without repetition

From: Jos (10584)

Date: 26 Nov, 2009 08:56:02

Message: 7 of 8

"uri " <sshangover@hotmail.com> wrote in message <helbpn$krj$1@fred.mathworks.com>...
> TideMan <mulgor@gmail.com> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9871@r24g2000prf.googlegroups.com>...
> > On Nov 26, 8:25?pm, "uri " <sshango...@hotmail.com> wrote:
> > > "Matt Fig" <spama...@yahoo.com> wrote in message <gsv7cj$oj...@fred.mathworks.com>...
> > > > Check out randperm.
> > >
> > > nice approach but not good enough, I need a small number of elmennts reletively to the maximum sampling size. if I'd use randperm it'll blow my memory
> >
> > Huh???
> > tic;y=randperm(200);toc
> > Elapsed time is 0.001623 seconds.
> >
> > Did you read the help correctly?
> > Or is there something that you're not telling us?
>
> let's say I have a vector (file wise) on my harddisk which has 10^8 cells
> I"d like to sample m elements out of this vector but using:
> gen=randperm(10^8);
> x=gen(1:m);
> gen is too big
> I just need the m elements without generating all the 10^8 numbers

if m << 10^8 you could just use a simple trial and error approach:

n = 1e8 ;
m = 200;
tic ;
Done = false ;
j = 0 ;
while ~Done,
   j = j + 1 ;
   R = ceil(n*rand(m,1)) ;
   Done = numel(unique(R))==m ;
end
fprintf('\nFound %d unique elements between 1 and %.0e\n in %.2fs,in %d trial(s)\n',m,n,toc,j) ;


Jos

Subject: Random numer generation without repetition

From: John D'Errico

Date: 26 Nov, 2009 10:15:07

Message: 8 of 8

"Jos (10584) " <#10584@fileexchange.com> wrote in message <helfr2$soh$1@fred.mathworks.com>...
> "uri " <sshangover@hotmail.com> wrote in message <helbpn$krj$1@fred.mathworks.com>...
> > TideMan <mulgor@gmail.com> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9871@r24g2000prf.googlegroups.com>...
> > > On Nov 26, 8:25?pm, "uri " <sshango...@hotmail.com> wrote:
> > > > "Matt Fig" <spama...@yahoo.com> wrote in message <gsv7cj$oj...@fred.mathworks.com>...
> > > > > Check out randperm.
> > > >
> > > > nice approach but not good enough, I need a small number of elmennts reletively to the maximum sampling size. if I'd use randperm it'll blow my memory
> > >
> > > Huh???
> > > tic;y=randperm(200);toc
> > > Elapsed time is 0.001623 seconds.
> > >
> > > Did you read the help correctly?
> > > Or is there something that you're not telling us?
> >
> > let's say I have a vector (file wise) on my harddisk which has 10^8 cells
> > I"d like to sample m elements out of this vector but using:
> > gen=randperm(10^8);
> > x=gen(1:m);
> > gen is too big
> > I just need the m elements without generating all the 10^8 numbers
>
> if m << 10^8 you could just use a simple trial and error approach:
>
> n = 1e8 ;
> m = 200;
> tic ;
> Done = false ;
> j = 0 ;
> while ~Done,
> j = j + 1 ;
> R = ceil(n*rand(m,1)) ;
> Done = numel(unique(R))==m ;
> end
> fprintf('\nFound %d unique elements between 1 and %.0e\n in %.2fs,in %d trial(s)\n',m,n,toc,j) ;
>
>
> Jos

Exactly. When the sample size is small, just reject
those samples that would have seen a repeat, and
redo the few that fail.

Of course, if the sample size is too large, then this
loop will essentially never terminate. So you need
to be careful there. A simple, lazy, scheme might
use a limit on the number of times the above while
loop will be allowed to run. If it fails without success
after say 5 times, then try a different method to
generate the samples.

John

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com