Re: Shuffling a linked list



Hi,

Richard Heathfield wrote:
Just use the standard shuffling algorithm, mildly adapted for lists.

Point one of your pointers, P1, to the first unprocessed item in the list,
item I. (There are 32 - I unprocessed items.) Pick a random number in the
range 1 to 32 - I. Counting the item pointed to by P1 as 1, keep counting
along the list, using P2 to point, until you hit the random number you
generated. Swap the data at P1 with the data at P2. Now move P1 to point to
the next item on the list. Iterate until done. (When P1 is NULL - because
it fell off the end of the list - you're done.)

thank you, I'll probably do that.

Even though the O'Reilly book "Perl Cookbook" states
in the chapter "Picking a Random Line from a File" that
swapping elements isn't evenly random (sorry don't have
the text right now)

Regards
Alex

--
http://preferans.de

.



Relevant Pages

  • Re: From file to char* array
    ... Richard Heathfield yazdi: ... whole bunch of pointers all pointing to the same place. ... one of those pointers to printf, you'll get the same data printed out. ...
    (comp.lang.c)
  • Re: obs function
    ... > well written C rarely needs pointers. ... I can't imagine Richard Heathfield saying this. ... However we don't know at compile time how many employees we will have. ... array of N employees using malloc, ...
    (comp.lang.c)
  • Re: Question about a struct declaration
    ... Richard Heathfield wrote: ... Richard doesn't want you not to use typedef'd pointers like that, ... he wants you to typedef the pointer as typedef T ... You all think I'm paranoid, ...
    (comp.lang.c)
  • Re: Pointers in C
    ... Richard Heathfield wrote: ... No, the behaviour will still be undefined, for two separate reasons. ... addresses for C pointers, and performs subtraction of pointers to ... distinct objects as if they were in the same object, ...
    (comp.lang.c)