Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6534673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:16:43+00:00 2026-05-25T10:16:43+00:00

I came across this following analysis of shuffling algorithms: Q: Given an array of

  • 0

I came across this following analysis of shuffling algorithms:

Q: Given an array of distinct integers, give an algorithm to randomly
reorder the integers so that each possible reordering is equally
likely. In other words, given a deck of cards, how can you shuffle
them such that any permutation of cards is equally likely?

Good answer: Go through the elements in order, swapping each element with a
random element in the array that does not appear earlier than the
element. This takes O(n) time. Note that there are several possible
solutions to this problem, as well as several good‐looking answers
that are incorrect. For example, a slight modification to the above
algorithm whereby one switches each element with any element in the
array does not give each reordering with equally probability.

What I would like to know is why switching each element with any other element in the array does not produce a good shuffle as opposed to using the Knuth shuffle (which is described). Also, how does the Knuth shuffle select values with equal probability? Any math or proof is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:16:44+00:00Added an answer on May 25, 2026 at 10:16 am

    The easiest proof that this algorithm does not produce a uniformly random permutation

    for (int i = 0; i < 3; ++i) {
       swap(a[i], a[rand() % 3]);
    }
    

    Is that it generates 27 possible outcomes, but there are only 3! = 6 permutations. Since 6 does not divide 27, there must be some permutation is that is picked too much, and some that is picked to little.

    Why is an O(n) algorithm optimal? Well, a random shuffle must touch every input sometimes (to change them), so any optimal algorithm needs to do at least O(n) work.

    Why is the Knuth algorithm correct? That requires a little bit more insight. You can prove via induction that the first item is selected with the correct probability (each item is equally likely to be first), and then prove that the inductive step holds as you advance through the loop, that the second, third, etc items are also selected with the correct probability from the remaining portions of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this in the following context from B. Pfaff's Analysis of Integrated
I came across this post , which reports the following interview question: Given two
I came across this post on the MSMobiles.com blog that says the following, among
i was googling when i came across this.. the following image & never before
I came across this situation where the following plinq statement inside static constructor gets
I've been looking at this question and came across the following code in one
I came across this following code: #include<stdio.h> #define d(x) x(#x[3]) int main(){ d(putchar); }
I came across this following arithmetic problem. But the result is different from normal
While reading the web2py manual, I came across this following: 'Once a new user
Came across this error today. Wondering if anyone can tell me what it means:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.