Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random numer generation without repetition

10 views
Skip to first unread message

Kishore

unread,
Apr 25, 2009, 4:52:02 AM4/25/09
to
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!

dpb

unread,
Apr 25, 2009, 10:31:20 AM4/25/09
to
> code to check for repetition....

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


--

Matt Fig

unread,
Apr 25, 2009, 10:40:19 AM4/25/09
to
Check out randperm.

uri

unread,
Nov 26, 2009, 2:25:04 AM11/26/09
to
"Matt Fig" <spam...@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

TideMan

unread,
Nov 26, 2009, 2:40:46 AM11/26/09
to
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?

uri

unread,
Nov 26, 2009, 2:47:03 AM11/26/09
to
TideMan <mul...@gmail.com> wrote in message <9983dcb2-dc04-45fe...@r24g2000prf.googlegroups.com>...

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

Jos (10584)

unread,
Nov 26, 2009, 3:56:02 AM11/26/09
to
"uri " <sshan...@hotmail.com> wrote in message <helbpn$krj$1...@fred.mathworks.com>...

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

John D'Errico

unread,
Nov 26, 2009, 5:15:07 AM11/26/09
to
"Jos (10584) " <#10...@fileexchange.com> wrote in message <helfr2$soh$1...@fred.mathworks.com>...

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

0 new messages