[vox-tech] shuffling arrays in C?

Rod Roark vox-tech@lists.lugod.org
Mon, 25 Nov 2002 11:25:53 -0800


Doesn't sound too tough to write your own algorithm.  For
example you can iterate through your array and swap each=20
element in turn with another randomly chosen element.

If the elements are large, it will be helpful (faster) to
build an array of pointers to the elements in the main=20
array, and then just shuffle that.

Details are left as an exercise for the reader.  :-)

-- Rod
   http://www.sunsetsystems.com/

On Monday 25 November 2002 11:05 am, Alexandra Thorn wrote:
> I've been looking around for a C library function that will shuffle the
> elements of an arbitrarily long array.  I'd been hoping to turn up
> something that would randomly shuffle the elements of an array.  The ar=
ray
> that I want to shuffle is made up of a class of structs that I've creat=
ed.
>
> A few hours of googling and fiddling with the man pages hasn't shed too
> much light on the issue: the closest I've found was strfry, which is
> supposed to randomize the order of a string.  I'm still a newbie, thoug=
h,
> so maybe don't know where to look.
>
> I suppose that I might be able to hack something up to randomize the or=
der
> of an array by using strfry to scramble an array of chars corresponding=
 to
> indices, but wanted to check if there is a better way to do this.