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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:19:35+00:00 2026-06-02T12:19:35+00:00

I am using this code to generate a random permutation of a vector using

  • 0

I am using this code to generate a random permutation of a vector using a variation of Fisher-Yates randomization algorithm (I am going from the first element to the last, not the other way around). I am using a boost::random::mt11213b RNG globally in a program that is seeded with generator.seed(time(NULL)); when program starts, hence a wrapper singleton RandomNumber here.

boost::random::uniform_int_distribution<unsigned long> 
    distribution(0, vec.size()-1);

for (unsigned long i=0;i<vec.size();i++)
    std::swap(vec[i], vec[distribution(RandomNumber::getInstance().generator)]);

Some experiments have led me to believe that there may be an issue in this algorithm, in a nutshell. This is what I did

  1. Created a vector of integer with length 100
  2. Filled first 75 elements with 0 and the last 25 with 1
  3. Shuffled an array.
  4. Took the first 5 elements from the list and summed them.

I repeated this procedure a few thousand times (with a loop, not by hand :)) each time starting with a fresh vector. Then I computed the arithmetic mean of the sums and it came about 0.98 instead of the expected 1.25.

The funny thing is that if I start with a vector that has been shuffled once with the same algorithm instead of an ordered one, the result increases to 1.22 and if I don’t discard the vector on each iteration but rather just shuffle it again, the result is around 1.25that is the expected value.

I am unsure as to what could be wrong. The algorithm looks sound, the only thing that I can think of that could have gone wrong is the seeding phase and the

boost::random::uniform_int_distribution<unsigned long> 
    distribution(0, vec.size()-1);

line that is called each time before a vector is shuffled (perhaps it should only be called once a program but that doesn’t make mush sense)

Any help will be 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-06-02T12:19:36+00:00Added an answer on June 2, 2026 at 12:19 pm

    If I had to take a guess as to the cause, you’re not changing the distribution size each time around the loop. The Art of Computer Programming algorithm is here.

    Once you shuffle up to n elements, you don’t want to touch the first n again, because repeated applications of pseudo random numbers don’t making things more random, they make them less random.

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

Sidebar

Related Questions

I am using this code to generate random text : from collections import defaultdict,
I am using this line of code to generate a random list of integers:
I am using this code to using a Random generator to generate random positions.
I must generate a random number using boost libraries, I use this code: boost::mt19937
I've been using this code to generate a random number with js: var max
This is the code that I'm using to generate a DH keypair: KeyPairGenerator keyGen
I wanted to generate a list view using below code. But after running this
When using this code (simplified for asking): var rows1 = (from t1 in db.TABLE1
I'm using the following code to generate a random string: <?php function random_string( )
I am using Python to generate some data and have some code like this

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.