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 239715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:36:27+00:00 2026-05-11T20:36:27+00:00

It seems that this simple shuffle algorithm will produce biased results: # suppose $arr

  • 0

It seems that this simple shuffle algorithm will produce biased results:

# suppose $arr is filled with 1 to 52

for ($i < 0; $i < 52; $i++) { 
  $j = rand(0, 51);

  # swap the items

  $tmp = $arr[j];
  $arr[j] = $arr[i];
  $arr[i] = $tmp;
}

You can try it… instead of using 52, use 3 (suppose only 3 cards are used), and run it 10,000 times and tally up the results, you will see that the results are skewed towards certain patterns…

The question is… what is a simple explanation for why it will happen?

The correct solution is to use something like

for ($i < 0; $i < 51; $i++) {  # last card need not swap 
  $j = rand($i, 51);        # don't touch the cards that already "settled"

  # swap the items

  $tmp = $arr[j];
  $arr[j] = $arr[i];
  $arr[i] = $tmp;
}

But the question is… why does the first method, seemingly also totally random, make the results biased?

Update 1: thanks for folks here pointing out that it needs to be rand($i, 51) for it to shuffle correctly.

  • 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-11T20:36:28+00:00Added an answer on May 11, 2026 at 8:36 pm

    Here’s the complete probability tree for these replacements.

    Let’s assume that you start with the sequence 123, and then we’ll enumerate all the various ways to produce random results with the code in question.

    123
     +- 123          - swap 1 and 1 (these are positions,
     |   +- 213      - swap 2 and 1  not numbers)
     |   |   +- 312  - swap 3 and 1
     |   |   +- 231  - swap 3 and 2
     |   |   +- 213  - swap 3 and 3
     |   +- 123      - swap 2 and 2
     |   |   +- 321  - swap 3 and 1
     |   |   +- 132  - swap 3 and 2
     |   |   +- 123  - swap 3 and 3
     |   +- 132      - swap 2 and 3
     |       +- 231  - swap 3 and 1
     |       +- 123  - swap 3 and 2
     |       +- 132  - swap 3 and 3
     +- 213          - swap 1 and 2
     |   +- 123      - swap 2 and 1
     |   |   +- 321  - swap 3 and 1
     |   |   +- 132  - swap 3 and 2
     |   |   +- 123  - swap 3 and 3
     |   +- 213      - swap 2 and 2
     |   |   +- 312  - swap 3 and 1
     |   |   +- 231  - swap 3 and 2
     |   |   +- 213  - swap 3 and 3
     |   +- 231      - swap 2 and 3
     |       +- 132  - swap 3 and 1
     |       +- 213  - swap 3 and 2
     |       +- 231  - swap 3 and 3
     +- 321          - swap 1 and 3
         +- 231      - swap 2 and 1
         |   +- 132  - swap 3 and 1
         |   +- 213  - swap 3 and 2
         |   +- 231  - swap 3 and 3
         +- 321      - swap 2 and 2
         |   +- 123  - swap 3 and 1
         |   +- 312  - swap 3 and 2
         |   +- 321  - swap 3 and 3
         +- 312      - swap 2 and 3
             +- 213  - swap 3 and 1
             +- 321  - swap 3 and 2
             +- 312  - swap 3 and 3
    

    Now, the fourth column of numbers, the one before the swap information, contains the final outcome, with 27 possible outcomes.

    Let’s count how many times each pattern occurs:

    123 - 4 times
    132 - 5 times
    213 - 5 times
    231 - 5 times
    312 - 4 times
    321 - 4 times
    =============
         27 times total
    

    If you run the code that swaps at random for an infinite number of times, the patterns 132, 213 and 231 will occur more often than the patterns 123, 312, and 321, simply because the way the code swaps makes that more likely to occur.

    Now, of course, you can say that if you run the code 30 times (27 + 3), you could end up with all the patterns occuring 5 times, but when dealing with statistics you have to look at the long term trend.

    Here’s C# code that explores the randomness for one of each possible pattern:

    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<String, Int32> occurances = new Dictionary<String, Int32>
            {
                { "123", 0 },
                { "132", 0 },
                { "213", 0 },
                { "231", 0 },
                { "312", 0 },
                { "321", 0 }
            };
    
            Char[] digits = new[] { '1', '2', '3' };
            Func<Char[], Int32, Int32, Char[]> swap = delegate(Char[] input, Int32 pos1, Int32 pos2)
            {
                Char[] result = new Char[] { input[0], input[1], input[2] };
                Char temp = result[pos1];
                result[pos1] = result[pos2];
                result[pos2] = temp;
                return result;
            };
    
            for (Int32 index1 = 0; index1 < 3; index1++)
            {
                Char[] level1 = swap(digits, 0, index1);
                for (Int32 index2 = 0; index2 < 3; index2++)
                {
                    Char[] level2 = swap(level1, 1, index2);
                    for (Int32 index3 = 0; index3 < 3; index3++)
                    {
                        Char[] level3 = swap(level2, 2, index3);
                        String output = new String(level3);
                        occurances[output]++;
                    }
                }
            }
    
            foreach (var kvp in occurances)
            {
                Console.Out.WriteLine(kvp.Key + ": " + kvp.Value);
            }
        }
    }
    

    This outputs:

    123: 4
    132: 5
    213: 5
    231: 5
    312: 4
    321: 4
    

    So while this answer does in fact count, it’s not a purely mathematical answer, you just have to evaluate all possible ways the random function can go, and look at the final outputs.

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

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The code below worked fine for me in Visual Studio… May 12, 2026 at 12:13 am
  • Editorial Team
    Editorial Team added an answer I think joeslice's answer will only give half the results.… May 12, 2026 at 12:13 am
  • Editorial Team
    Editorial Team added an answer try this. use group by with the name of the… May 12, 2026 at 12:13 am

Related Questions

Yes, I realize this question was asked and answered, but I have specific questions
It seems that this should be simple, but I'm having trouble figuring out how
I'm trying to learn RegEx in Ruby, based on what I'm reading in The
It seems that the choice to use string parsing vs. regular expressions comes up

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.