Re: Random numer generation without repetition
- From: "John D'Errico" <woodchips@xxxxxxxxxxxxxxxx>
- Date: Thu, 26 Nov 2009 10:15:07 +0000 (UTC)
"Jos (10584) " <#10584@xxxxxxxxxxxxxxxx> wrote in message <helfr2$soh$1@xxxxxxxxxxxxxxxxxx>...
"uri " <sshangover@xxxxxxxxxxx> wrote in message <helbpn$krj$1@xxxxxxxxxxxxxxxxxx>...
TideMan <mulgor@xxxxxxxxx> wrote in message <9983dcb2-dc04-45fe-af74-71e66d7a9871@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>...
On Nov 26, 8:25?pm, "uri " <sshango...@xxxxxxxxxxx> wrote:
"Matt Fig" <spama...@xxxxxxxxx> wrote in message <gsv7cj$oj...@xxxxxxxxxxxxxxxxxx>...
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
.
- References:
- Re: Random numer generation without repetition
- From: TideMan
- Re: Random numer generation without repetition
- From: uri
- Re: Random numer generation without repetition
- From: Jos (10584)
- Re: Random numer generation without repetition
- Prev by Date: Re: sampling without repetition
- Next by Date: Re: help
- Previous by thread: Re: Random numer generation without repetition
- Next by thread: sampling without repetition
- Index(es):
Relevant Pages
|