5

Is there a way to randomly shuffle an array using only a source of random boolean values? SO to clarify, shuffle using true /false only, and not integers or decimals.

For this question, I'm specifically excluding an approach whereby the booleans are just arranged into an integer and then it's used in a Fisher-Yates shuffle. I'd be repeatedly shuffling approximately 30,000 items and have a ready source of random booleans. I'd need to build up a very large integer for Fischer-Yates to avoid any form of bias in the shuffle. Can this step be avoided and true /false used directly somehow? I have the sense that some soft of bubble sort might do it, with the comparison determined randomly, but I can't quite flesh it out.

I'd be interested in any algorithm than wasn't ridiculous i.e. with a stupid time complexity of something like 2^O(n). This question builds upon some of the concepts of Best random permutation employing only one random number but isn't identical.

Paul Uszak
  • 1,602
  • 1
  • 13
  • 21

4 Answers4

7

No, there is no algorithm that shuffles an array of length $n > 2$ using a bounded number of random Booleans. This is because given an algorithm that uses $m$ random bits (at most), each outcome has a probability of the form $A/2^m$, whereas we need each possible permutation to have probability $1/n!$.

When you're using Fisher–Yates shuffle in the form you describe you aren't getting a uniformly random permutation, though you are getting something very close. If you want to get a truly uniform random permutation then you need to use some form of rejection sampling.

Using bubble sort in the way you describe would likely be a disaster – it would likely result in a random permutation which is easy to distinguish from uniform. Don't do that. There are no tricks and shortcuts. If you want your random permutations to be faster at the cost of being slightly less uniform, you can reduce the precision involved in approximating random integers inside the Fisher–Yates shuffle. This will probably be much better than inventing your own algorithm.

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
3

Referring to a $b$-bit number as $b$ individual bits or vice-versa doesn't change the number of random bits you need. If you need a gazillion bits when you consider them as forming integers, you need a gazillion bits when you consider them as individual tosses of a fair coin.

David Richerby
  • 82,470
  • 26
  • 145
  • 239
0

You actually can use true / false to simulate a Riffle Shuffle or Faro Shuffle:

  1. Start with the deck of cards on the middle
  2. Move half of the deck to the left, and half to the right
  3. Get a boolean: if true, then move the top card from the left pile back to the middle, or if false, move that top card from the right pile back to the middle
  4. Repeat step 3 until all cards are back to the middle

There can be a variation of step 3: if another boolean is true, move 1 card, otherwise, move 2 cards.

Another variation:

  1. Start with the deck of cards on the middle
  2. Get a boolean: if true, then move the top card to the left pile, or if false, move that top card to the right pile
  3. Repeat step 2 until all cards in the middle are gone.
  4. Now merge the left and right pile of cards by taking 1 card from each pile alternatively and putting them back into one pile

You can also use boolean to say, move 1 card vs 2 cards in step 2. Also a boolean for step 4: if true, move one card from the left pile card during merging, but if false, moving one card from the right pile card during merging instead.

But you probably have to do it 7, 8 times to get a pretty good shuffle.

nonopolarity
  • 413
  • 3
  • 11
0

Why not converting booleans to integer, and using Fischer-Yates ?

If you have n elements, you will have n! possible permutations.

Let us use FYK algorithm (supposing it is almost good).

Then you will need:

  • choose among n : say n = 2^k_n
  • then choose among n-1 : says n=2^k_n-1
  • etc.

So you need sum(k_n,k_n-1, ...) = P, where 2^P=n! (approximately, because every n, n-1, ... is not a power of 2.

1 So you need P booleans, where 2^P=n!

2 FYK needs almost minimal time: you have to make n changes (difficult to do less). and cutting/converting bits is minimal. You even can struct your array in some of binary tree.