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.
> 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)
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?
TideMan <mul...@gmail.com> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9...@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
"uri " <sshango...@hotmail.com> wrote in message <helbpn$kr...@fred.mathworks.com>... > TideMan <mul...@gmail.com> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9...@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) ;
> "uri " <sshango...@hotmail.com> wrote in message <helbpn$kr...@fred.mathworks.com>... > > TideMan <mul...@gmail.com> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9...@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.