Re: Randomly outputting an array



In article <Pine.LNX.4.60-041.0607161411410.6121
@unix43.andrew.cmu.edu>, ajonospam@xxxxxxxxxxxxxx says...

Picking N pairs uniformly at random and swapping them will definitely
not shuffle the array, since there are (N^2)^N = N^(2N) ways to pick
N pairs, and N! ways to shuffle the array, and N^(2N) is generally not
a multiple of N!.

However, if you carefully pick those N pairs so that the first swap
swaps a random element into position 1, the second swap swaps a random
element /from those that remain/ into position 2, and so on, then that's
the "52-card pickup" algorithm, and it works fine. It's all in /which/
N pairs you swap.

You're right, my method doesn't work. I can't quite see what is
logically wrong with it, but if it makes n^n possible results, n! isn't
going to divide exactly into it.

- Gerry Quinn
.



Relevant Pages

  • Re: Newbee Question about random numbers
    ... This is a school project that I'm working on. ... > the words in array mixed up. ... you swap the elements at each index of the ... array with another random element, ...
    (comp.lang.java.programmer)
  • Re: shuffle.... isnt
    ... The concept was to grab all of the cards that have expired id's and ... put them into one array. ... random element. ... Shuffle is a rather expensive operation, ...
    (comp.lang.php)
  • Re: Idea for algo.
    ... quite a simple exchange (swap) of the byte involved with 2 others. ... it is actually a bit more involved, since you must shuffle 2 arrays, the ... > Peter wrote: ... >> you do not need to shuffle the whole array! ...
    (sci.crypt)
  • Re: Constant array
    ... But I can't quite figure out how to make an array of constants without ... DOES> SWAP CELLS + @ EXECUTE; ...
    (comp.lang.forth)
  • Re: "Sorting" assignment
    ... you think using memcpy a block at a time is not the best way to swap ... trouble now or later. ... It does not bother to zero the array (you'd need a clever optimiser ... void memswap(char *ptrToA, char *ptrToB, int intLength) ...
    (comp.programming)