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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:52:20+00:00 2026-06-18T11:52:20+00:00

As many people know, Python uses the Mersenne Twister (MT) algorithm to handle its

  • 0

As many people know, Python uses the Mersenne Twister (MT) algorithm to handle its random numbers. However, despite having a very long period (~2^19937), it is also well-known that you can’t reach every random permutation when you shuffle a sequence greater than 2080 elements (since !2081 > 2^19937). As I am dealing with permutations and statistical properties are important to me, I’m trying to figure out the best way to mix or re-seed the Python generator with an additional source of randomness to avoid repetition.

Currently, my concept is to use the system random number generator (SystemRandom) to add an external source of randomness to the MT generator. There are two ways that I can think of to do this:

  1. XOR the SystemRandom random number with the MT random number
  2. Use the SystemRandom to reseed the MT

The first approach is used with some frequency by hardware random number generators, to reduce their bias tendencies. However, it’s wildly inefficient. On a Windows XP machine, the SystemRandom is 50 times slower than the standard Python random function. That’s a huge performance hit when most of your function involves shuffling. Given that, reseeding the MT with the SystemRandom should be significantly more efficient.

However, there are two issues with that approach also. Firstly, reseeding the MT during operation might disrupt its statistical properties. I’m fairly certain this shouldn’t be an issue if the MT runs long enough, as each run of MT values should be well-formed (regardless of the starting point). It does however indicate that a sizable period between MT reseeding is preferred. Secondly, there is the question of what is the most efficient way to trigger reseeding. The simplest way to handle this is with a counter. However, more efficient ways might be possible.

So then, there are three questions on this point:

  1. Has anyone read anything to the effect that reseeding an MT with a random value after every N samples will alter its desirable statistical properties?
  2. Does anyone know a more efficient way to do so than incrementing a counter to trigger a reseed?
  3. Finally, if anyone knows a generally better way to approach this problem, I’m all ears.
  • 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-18T11:52:22+00:00Added an answer on June 18, 2026 at 11:52 am

    Reseeding will not help you. It will just jump somewhere else into the (very very) long MT sequence. Are-you sure shuffling your data gives you a biased result? Because you will never have enough hours in the universe lifetime to generate all possible sequences. So even if you know that some sequences can possibly never be generated, it doesn’t mean that generated sequences will be biased. I guess your best bet is simply to use the shuffle command as it.


    If you look at numpy.random.shuffle source code (line 4376), here is basically the algorithm used (I simplified it for clarity):

    i = len(x) - 1
    while i > 0:
        j = randint(0, i)
        x[i], x[j] = x[j], x[i]
        i = i - 1
    

    In other words, beginning from the end, it swaps the value with a random value taken randomly before it in the array, until all values are swapped. The final state not only depends on the random generator, but also on the initial state of the array. It means that in theory, you should be able to visit all permutations, if you perform enough shuffles.

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

Sidebar

Related Questions

I know many people have asked, this question, but despite hardcoding the path to
I know many people ask this but all the answers are specific apps so
I use VIM editor for PHP, i know many people will point to PDT
I know many people experience this problem, but the solutions I found online do
I saw this related question about publishing toolchain but I know many people did
I know that many people think that wiping the RAM at shutdown is fairly
I don't know how many people have used this library, but I am using
I know there have been many people asking the same thing, I have read
I know that many people use Sinatra when programming webapps in Ruby because they
I normally code admin scripts in Python and I know of many that code

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.