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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:16:11+00:00 2026-05-28T04:16:11+00:00

The classic Fisher Yates looks something like this: void shuffle1(std::vector<int>& vec) { int n

  • 0

The classic Fisher Yates looks something like this:

void shuffle1(std::vector<int>& vec)
{
    int n = vec.size();
    for (int i = n - 1; i > 0; --i)
    {
        std::swap(vec[i], vec[rand() % (i + 1)]);
    }
}

Yesterday, I implemented the iteration “backwards” by mistake:

void shuffle2(std::vector<int>& vec)
{
    int n = vec.size();
    for (int i = 1; i < n; ++i)
    {
        std::swap(vec[i], vec[rand() % (i + 1)]);
    }
}

Is this version in any way worse (or better) than the first? Does it skew the resulting probabilities?

  • 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-28T04:16:11+00:00Added an answer on May 28, 2026 at 4:16 am

    Yes it’s even distribution assuming rand() is. We will prove this by showing that each input can generate each permutation with equal probability.

    N=2 can be easily proven.
    We will draw it as a tree where the the children represent each string you can get by inserting the character after comma into the leftmost string.

      0,1   //input where 0,1 represent indices
    01  10  //output. Represents permutations of 01. It is clear that each one has equal probability
    

    For N, we will have every permutations for N-1, and randomly swapping the last character for N

        (N-1 0th permutation),N     .....          (N-1 Ith permutation),N ________________________  
          /              \                       /                   \                             \ 
    0th permutation of N  1st permutation....   (I*N)th permutation   ((I*N)+1)th permutation .... (I*N)+(I-1)th permutation
    

    This shitty induction should lead you to it having even distribution.


    Example:

    N=2:

      0,1
    01  10 // these are the permutations. Each one has equal probability
    

    N=3:

               0,1|2           // the | is used to separate characters that we will insert later
        01,2           10,2    // 01, 10 are permutations from N-1, 2 is the new value
     210 021 012   201 120 102 // these are the permutations, still equal probability
    

    N=4: (curved to aid reading)

                                                               0,1|23
    
                                                           01,2|3  10,2|3
    
                                               012,3 021,3 210,3    102,3 120,3 201,3
    
    0123 0132 0321 3230                                                                                  2013 2031 2310 3012
                        0213 0231 0312 3210                                          1203 1230 1302 3201
                                            2103 2130 2301 3102  1023 1032 1320 3021
    

    etc

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

Sidebar

Related Questions

The classic of writing a singleton in java is like this: public class SingletonObject
Classic ASP creates cookies with name something like ASPSESSIONIDSSDSQQCR where suffix after ASPSESSIONID is
Classic example of a simple server: class ThreadPerTaskSocketServer { public static void main(String[] args)
Apologies if this is classic ASP 101, but its been so long since I
Classic ASP, VBScript context. A lot of articles including this Microsoft one , say
Using classic asp, I am trying to query a SQL Server database like so:
Another classic ant junit problem. Like the other questions I found on the topic,
I have this classic ASP site which has been working fine until we updated
I have a classic ASP forum, I'd like to use a master page and
Reading this old but classic document Writing High-Performance Managed Applications - A Primer ,

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.