help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Randomly generate 10 different integers


From: Dupuis
Subject: Re: Randomly generate 10 different integers
Date: Wed, 16 Sep 2009 03:22:16 -0700 (PDT)



Xin Dong wrote:
> 
> Hi,
> 
> I wanna randomly generate 10 different integers in a given range. I know I
> can use random function and discard duplicate values. But is there any
> other
> simple way to do it?
> 
> 

What you're searching for is an implementation of the Fisher-Yates
algortihm, see http://en.wikipedia.org/wiki/Fisher–Yates_shuffle

Here is a possible way:
x=(1:10); n = length(x);
while n >= 2,
 t = ceil(unifrnd(0, n)); %# returns 1 <= t <= n
 if (n != t), dummy = x(n); x(n) = x(t); x(t) = dummy; endif %# permute
 n = n - 1;
endwhile 
-- 
View this message in context: 
http://www.nabble.com/Randomly-generate-10-different-integers-tp24277099p25469653.html
Sent from the Octave - General mailing list archive at Nabble.com.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]