Xref: utzoo comp.sources.wanted:7421 alt.msdos.programmer:44 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!uwvax!tank!eecae!cps3xx!cpsvax!cook From: cook@cpsvax.cps.msu.edu (Tom Cook) Newsgroups: comp.sources.wanted,alt.msdos.programmer Subject: Re: Need algorithm to scramble order of array Message-ID: <3016@cps3xx.UUCP> Date: 17 May 89 19:46:34 GMT References: <3008@cps3xx.UUCP> Sender: usenet@cps3xx.UUCP Reply-To: cook@cpsvax.UUCP (Tom Cook) Organization: Michigan State University, Computer Science Department 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. Right now I am just using a random number generator to form >a list of numbers that directly determines the order of the array like this: > > n = size of array I usually use something like this: for i = n to 2 step -1 do begin x = random number between 1 and i swap array[i] and array[x] end This loop will always execute exactly n-1 times. >Paul Reed >Internet: reedp@horus.cem.msu.edu Bitnet: reedp@MSUCEM Tom Cook cook@cpsvax.msu.edu