Xref: utzoo comp.sources.wanted:7419 alt.msdos.programmer:43 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!indri!ames!elroy!ucla-cs!uci-ics!posert From: posert@ics.uci.edu (Bob Posert) Newsgroups: comp.sources.wanted,alt.msdos.programmer Subject: Re: Need algorithm to scramble order of array Summary: O(n) algorithm Message-ID: <14831@paris.ics.uci.edu> Date: 17 May 89 18:26:38 GMT References: <3008@cps3xx.UUCP> Sender: news@paris.ics.uci.edu Reply-To: Bob Posert Organization: University of California, Irvine - Dept of ICS Lines: 21 In article <3008@cps3xx.UUCP> reedp@frith.egr.msu.edu () writes: >I need a fast algorithm to randomly rearrange the order of elements in >an array. [...] > n = size of array The algorithm I usually use is: {Get all numbers in the array} for i = 1 to N do a[i] = i endfor {Scramble them} for i = 1 to N do j = { random number, 1 <= j <= N } swap(a[i], a[j]); endfor -- Bob Posert I'm: posert@bonnie.ics.uci.edu or {sdcsvax|ucbvax}!ucivax!bonnie!posert