Friday, 26 August 2011

Generating a unique ordering of random integers starting from 1

randsrc with MATLAB picks random items from an alphabet but does not make sure that the picks are unique. If you need uniqueness and your alphabet is the set of integers between 1 and limit, here's a very simple way to do it:
[temp A] = sort(rand(1,limit),2);
A contains a random ordering of integers from 1 to limit. The idea is to generate uniformly distributed random numbers between 0 and 1, sort them and get the sorted indices. Very simple.

PS: I got this idea from here.

No comments:

Post a Comment