[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ProgSoc] Randomisation



Chaos gurus (oxymoron?),

I have a number of entities (c structures actually), and I have to
randomly re-arrange their order. The randomisation should be without
bias.

The method I am using is taken from the book 'C: how to program' (PP
text, I forget the authors. Deitel?). I would like your comments on its
integrity, effectiveness and un-biasness. (wince :)

I know how many structures their are. I malloc space for an array of
pointers to these structures. I initialise the array of pointers to
point to the structures in the order that I read them from a file. Using
a for loop, I go through each and every pointer, in order, and swap it
with another pointer chosen randomly from the array (including itself).

I use 

        index = rand() % num_structures;

to pick the random index, and I have seeded the randomiser once before
beginning the process using

        srand(time(NULL));

Any comments appreciated, although please include explainations of your
thoughts.

j.

-- 
Don't stress it - it's only 1s and 0s.
--
You are subscribed to the progsoc mailing list. To unsubscribe, send a
message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.

This list is archived at <http://www.progsoc.uts.edu.au/lists/progsoc/>