Re: Randomize array order [message #55025 is a reply to message #55024] |
Thu, 26 July 2007 05:24   |
Conor
Messages: 138 Registered: February 2007
|
Senior Member |
|
|
On Jul 25, 3:59 pm, hradilv <hrad...@yahoo.com> wrote:
> On Jul 25, 2:17 pm, Conor <cmanc...@gmail.com> wrote:
>
>> Hi everyone!
>
>> Anyone know an efficient way to randomize an array (I have a
>> sorted array that I want unsorted). Initially, I tried something like
>> this:
>
>> array = findgen(1000000)
>> unsort = array[sort(randomu(seed,1000000))]
>
>> It works, but sorting on a million elements is rather slow. Anyone
>> know a faster way?
>
> Takes about 0.87 seconds on my machine? Is this too long?
>
> Of course, if you want "with replacement" you could do:
> unsort = array[long(1000000*randomu(seed,1000000))]
>
> But you probably want a permutation, right? That'll be more difficult.
It takes about 3 seconds on my machine (really crappy machine). The
whole program takes about 6 seconds, so about half the program
execution comes from the one sort statement. This is going to execute
somewhere between 1000 and 10,000 times, so losing three seconds will
save me between 50 minutes and 8 hours. Yeah, I need a permutation.
Losing or repeating elements would be bad. It's always got to be the
hard way, doesn't it :)
|
|
|