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

  • Home
  • SEARCH
  • 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 4007554
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:38:47+00:00 2026-05-20T08:38:47+00:00

The famous Fisher-Yates shuffle algorithm can be used to randomly permute an array A

  • 0

The famous Fisher-Yates shuffle algorithm can be used to randomly permute an array A of length N:

For k = 1 to N
    Pick a random integer j from k to N
    Swap A[k] and A[j]

A common mistake that I’ve been told over and over again not to make is this:

For k = 1 to N
    Pick a random integer j from 1 to N
    Swap A[k] and A[j]

That is, instead of picking a random integer from k to N, you pick a random integer from 1 to N.

What happens if you make this mistake? I know that the resulting permutation isn’t uniformly distributed, but I don’t know what guarantees there are on what the resulting distribution will be. In particular, does anyone have an expression for the probability distributions over the final positions of the elements?

  • 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-20T08:38:47+00:00Added an answer on May 20, 2026 at 8:38 am

    An Empirical Approach.

    Let’s implement the erroneous algorithm in Mathematica:

    p = 10; (* Range *)
    s = {}
    For[l = 1, l <= 30000, l++, (*Iterations*)
       a = Range[p];
       For[k = 1, k <= p, k++, 
         i = RandomInteger[{1, p}];
         temp = a[[k]];
         a[[k]] = a[[i]];
         a[[i]] = temp
       ];
       AppendTo[s, a];
    ]  
    

    Now get the number of times each integer is in each position:

    r = SortBy[#, #[[1]] &] & /@ Tally /@ Transpose[s]  
    

    Let’s take three positions in the resulting arrays and plot the frequency distribution for each integer in that position:

    For position 1 the freq distribution is:

    enter image description here

    For position 5 (middle)

    enter image description here

    And for position 10 (last):

    enter image description here

    and here you have the distribution for all positions plotted together:

    enter image description here

    Here you have a better statistics over 8 positions:

    enter image description here

    Some observations:

    • For all positions the probability of
      “1” is the same (1/n).
    • The probability matrix is symmetrical
      with respect to the big anti-diagonal
    • So, the probability for any number in the last
      position is also uniform (1/n)

    You may visualize those properties looking at the starting of all lines from the same point (first property) and the last horizontal line (third property).

    The second property can be seen from the following matrix representation example, where the rows are the positions, the columns are the occupant number, and the color represents the experimental probability:

    enter image description here

    For a 100×100 matrix:

    enter image description here

    Edit

    Just for fun, I calculated the exact formula for the second diagonal element (the first is 1/n). The rest can be done, but it’s a lot of work.

    h[n_] := (n-1)/n^2 + (n-1)^(n-2) n^(-n)
    

    Values verified from n=3 to 6 ( {8/27, 57/256, 564/3125, 7105/46656} )

    Edit

    Working out a little the general explicit calculation in @wnoise answer, we can get a little more info.

    Replacing 1/n by p[n], so the calculations are hold unevaluated, we get for example for the first part of the matrix with n=7 (click to see a bigger image):

    enter image description here

    Which, after comparing with results for other values of n, let us identify some known integer sequences in the matrix:

    {{  1/n,    1/n      , ...},
     {... .., A007318, ....},
     {... .., ... ..., ..},
     ... ....,
     {A129687, ... ... ... ... ... ... ..},
     {A131084, A028326 ... ... ... ... ..},
     {A028326, A131084 , A129687 ... ....}}
    

    You may find those sequences (in some cases with different signs) in the wonderful http://oeis.org/

    Solving the general problem is more difficult, but I hope this is a start

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

Sidebar

Related Questions

Can I make a windows application with Ruby? what famous software made with Ruby
Are there any named/famous/particularly good algorithms for organizing email messages into threads? I'm looking
Erlang with mnesia/dets is famous for it slow startup times after a crash. Basically
The question of whether P=NP is perhaps the most famous in all of Computer
Lets look around for famous programming languages and their runtime environments and frameworks... C,
The very famous ActionLink : <%: Html.ActionLink(Back to List, Index)%> Now, this link is
I made a website using the famous symfony framework. I wanted to add rich
The JVM (especially the HotSpot VM) is famous for having a huge number of
For example, Portable Executable has several, including the famous MZ at the beginning, as
When I have made simple single thread games I implemented the game logic in

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.