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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:07:54+00:00 2026-06-05T12:07:54+00:00

So I’ve got a custom randomizer class that uses a Mersenne Twister (the code

  • 0

So I’ve got a custom randomizer class that uses a Mersenne Twister (the code I use is adapted from this site). All seemed to be working well, until I started testing different seeds (I normally use 42 as a seed, to ensure that each time I run my program, the results are the same, so I can see how code changes influence things).

It turns out that, no matter what seed I choose, the code produces the exact same series of numbers each time. Clearly I’m doing something wrong, but I don’t know what. Here is my seed function:

void Randomizer::Seed(unsigned long int Seed)
{
    int ii;
    x[0] = Seed & 0xffffffffUL;
    for (ii = 0; ii < N; ii++)
    {
        x[ii] = (1812433253UL * (x[ii - 1] ^ (x[ii - 1] >> 30)) + ii);
        x[ii] &= 0xffffffffUL;
    }
}

And this is my Rand() function

unsigned long int Randomizer::Rand()
{
    unsigned long int Result;
    unsigned long int a;
    int ii;

    // Refill x if exhausted
    if (Next == N)
    {
        Next = 0;

        for (ii = 0; ii < N - 1; ii++)
        {
            Result = (x[ii] & U) | x[ii + 1] & L;
            a = (Result & 0x1UL) ? A : 0x0UL;
            x[ii] = x[( ii + M) % N] ^ (Result >> 1) ^ a;
        }

        Result = (x[N - 1] & U) | x[0] & L;
        a = (Result & 0x1UL) ? A : 0x0UL;
        x[N - 1] = x[M - 1] ^ (Result >> 1) ^ a;
    }
    Result = x[Next++];

    //Improves distribution
    Result ^= (Result >> 11);
    Result ^= (Result << 7) & 0x9d2c5680UL;
    Result ^= (Result << 15) & 0xefc60000UL;
    Result ^= (Result >> 18);

    return Result;
}

The various values are:

#define A 0x9908b0dfUL
#define U 0x80000000UL
#define L 0x7fffffffUL

int Randomizer::N = 624;
int Randomizer::M = 397;
int Randomizer::Next = 0;
unsigned long Randomizer::x[624];

Can anyone help me figure out why different seeds don’t result in different sequences of numbers?

  • 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-05T12:07:57+00:00Added an answer on June 5, 2026 at 12:07 pm

    Your Seed() function assigns to x[0], then starts looping at ii=0, which overwrites x[0] with an undefined value (it references x[-1]). Start your loop at 1, and you’ll probably be all set.

    Writing your own randomizer is dangerous. Why? It’s hard to get right (see above), it’s hard to know if you’ve done it right, and if it’s wrong, things that rely on correctly distributed random numbers will not work quite right. Hopefully that thing is not cryptography or statistical modeling where the tails matter…. Think about using std::random, or if you’re not on C++11 yet, boost::random.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.