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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:23:23+00:00 2026-06-18T12:23:23+00:00

I need a Random Number Generator that produces same sequence of numbers both in

  • 0

I need a Random Number Generator that produces same sequence of numbers both in iOS and Android if we give the same Seed in both.

I tried the rand() function with the srand(1000). But it gave different outputs.
Then I tried mersenne twister. But that too gave difference sequence for same seed.

Could any one please help me on this.

I am using cocos2d-x for my development.

  • 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-18T12:23:24+00:00Added an answer on June 18, 2026 at 12:23 pm

    I have adapted an online CRandomMersenne library and I am really sorry I can no longer find the source for that one. But here is my Mersenne Twister implementation:

    // Define 32 bit signed and unsigned integers.
    // Change these definitions, if necessary, to match a particular platform
    #if defined(_WIN16) || defined(__MSDOS__) || defined(_MSDOS) 
       // 16 bit systems use long int for 32 bit integer
       typedef long int           int32;   // 32 bit signed integer
       typedef unsigned long int  uint32;  // 32 bit unsigned integer
    #else
       // Most other systems use int for 32 bit integer
       typedef int                int32;   // 32 bit signed integer
       typedef unsigned int       uint32;  // 32 bit unsigned integer
    #endif
    
    // Define 64 bit signed and unsigned integers, if possible
    #if (defined(__WINDOWS__) || defined(_WIN32)) && (defined(_MSC_VER) || defined(__INTEL_COMPILER))
       // Microsoft and other compilers under Windows use __int64
       typedef __int64            int64;   // 64 bit signed integer
       typedef unsigned __int64   uint64;  // 64 bit unsigned integer
       #define INT64_DEFINED               // Remember that int64 is defined
    #elif defined(__unix__) && (defined(_M_IX86) || defined(_M_X64))
       // Gnu and other compilers under Linux etc. use long long
       typedef long long          int64;   // 64 bit signed integer
       typedef unsigned long long uint64;  // 64 bit unsigned integer
       #define INT64_DEFINED               // Remember that int64 is defined
    #else
       // 64 bit integers not defined
       // You may include definitions for other platforms here
    #endif
    
    void EndOfProgram(void);               // System-specific exit code (userintf.cpp)
    
    void FatalError(char * ErrorText);     // System-specific error reporting (userintf.cpp)
    
    class CRandomMersenne {                // Encapsulate random number generator
    #if 0
       // Define constants for type MT11213A:
    #define MERS_N   351
    #define MERS_M   175
    #define MERS_R   19
    #define MERS_U   11
    #define MERS_S   7
    #define MERS_T   15
    #define MERS_L   17
    #define MERS_A   0xE4BD75F5
    #define MERS_B   0x655E5280
    #define MERS_C   0xFFD58000
    #else    
       // or constants for type MT19937:
    #define MERS_N   624
    #define MERS_M   397
    #define MERS_R   31
    #define MERS_U   11
    #define MERS_S   7
    #define MERS_T   15
    #define MERS_L   18
    #define MERS_A   0x9908B0DF
    #define MERS_B   0x9D2C5680
    #define MERS_C   0xEFC60000
    #endif
    public:
       CRandomMersenne(uint32 seed) {      // Constructor
          RandomInit(seed); LastInterval = 0;}
       void RandomInit(uint32 seed);       // Re-seed
       void RandomInitByArray(uint32 seeds[], int length); // Seed by more than 32 bits
       int IRandom (int min, int max);     // Output random integer
       int IRandomX(int min, int max);     // Output random integer, exact
       double Random();                    // Output random float
       uint32 BRandom();                   // Output random bits
    private:
       void Init0(uint32 seed);            // Basic initialization procedure
       uint32 mt[MERS_N];                  // State vector
       int mti;                            // Index into mt
       uint32 LastInterval;                // Last interval length for IRandomX
       uint32 RLimit;                      // Rejection limit used by IRandomX
       enum TArch {LITTLE_ENDIAN1, BIG_ENDIAN1, NONIEEE}; // Definition of architecture
       TArch Architecture;                 // Conversion to float depends on architecture
    };    
    
    
    class CRandomMother {             // Encapsulate random number generator
    public:
       void RandomInit(uint32 seed);       // Initialization
       int IRandom(int min, int max);      // Get integer random number in desired interval
       double Random();                    // Get floating point random number
       uint32 BRandom();                   // Output random bits
       CRandomMother(uint32 seed) {   // Constructor
          RandomInit(seed);}
    protected:
       uint32 x[5];                        // History buffer
    };
    
    #endif
    
    void CRandomMersenne::Init0(uint32 seed) {
       // Detect computer architecture
       union {double f; uint32 i[2];} convert;
       convert.f = 1.0;
       if (convert.i[1] == 0x3FF00000) Architecture = LITTLE_ENDIAN1;
       else if (convert.i[0] == 0x3FF00000) Architecture = BIG_ENDIAN1;
       else Architecture = NONIEEE;
    
       // Seed generator
       mt[0]= seed;
       for (mti=1; mti < MERS_N; mti++) {
          mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
       }
    }
    
    void CRandomMersenne::RandomInit(uint32 seed) {
       // Initialize and seed
       Init0(seed);
    
       // Randomize some more
       for (int i = 0; i < 37; i++) BRandom();
    }
    
    
    void CRandomMersenne::RandomInitByArray(uint32 seeds[], int length) {
       // Seed by more than 32 bits
       int i, j, k;
    
       // Initialize
       Init0(19650218);
    
       if (length <= 0) return;
    
       // Randomize mt[] using whole seeds[] array
       i = 1;  j = 0;
       k = (MERS_N > length ? MERS_N : length);
       for (; k; k--) {
          mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) + seeds[j] + j;
          i++; j++;
          if (i >= MERS_N) {mt[0] = mt[MERS_N-1]; i=1;}
          if (j >= length) j=0;}
       for (k = MERS_N-1; k; k--) {
          mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i;
          if (++i >= MERS_N) {mt[0] = mt[MERS_N-1]; i=1;}}
       mt[0] = 0x80000000UL;  // MSB is 1; assuring non-zero initial array
    
       // Randomize some more
       mti = 0;
       for (int i = 0; i <= MERS_N; i++) BRandom();
    }
    
    
    uint32 CRandomMersenne::BRandom() {
       // Generate 32 random bits
       uint32 y;
    
       if (mti >= MERS_N) {
          // Generate MERS_N words at one time
          const uint32 LOWER_MASK = (1LU << MERS_R) - 1;       // Lower MERS_R bits
          const uint32 UPPER_MASK = 0xFFFFFFFF << MERS_R;      // Upper (32 - MERS_R) bits
          static const uint32 mag01[2] = {0, MERS_A};
    
          int kk;
          for (kk=0; kk < MERS_N-MERS_M; kk++) {    
             y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
             mt[kk] = mt[kk+MERS_M] ^ (y >> 1) ^ mag01[y & 1];}
    
          for (; kk < MERS_N-1; kk++) {    
             y = (mt[kk] & UPPER_MASK) | (mt[kk+1] & LOWER_MASK);
             mt[kk] = mt[kk+(MERS_M-MERS_N)] ^ (y >> 1) ^ mag01[y & 1];}      
    
          y = (mt[MERS_N-1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
          mt[MERS_N-1] = mt[MERS_M-1] ^ (y >> 1) ^ mag01[y & 1];
          mti = 0;
       }
    
       y = mt[mti++];
    
    #if 1
       // Tempering (May be omitted):
       y ^=  y >> MERS_U;
       y ^= (y << MERS_S) & MERS_B;
       y ^= (y << MERS_T) & MERS_C;
       y ^=  y >> MERS_L;
    #endif
    
       return y;
    }
    
    
    double CRandomMersenne::Random() {
       // Output random float number in the interval 0 <= x < 1
       union {double f; uint32 i[2];} convert;
       uint32 r = BRandom();               // Get 32 random bits
       // The fastest way to convert random bits to floating point is as follows:
       // Set the binary exponent of a floating point number to 1+bias and set
       // the mantissa to random bits. This will give a random number in the 
       // interval [1,2). Then subtract 1.0 to get a random number in the interval
       // [0,1). This procedure requires that we know how floating point numbers
       // are stored. The storing method is tested in function RandomInit and saved 
       // in the variable Architecture.
    
       // This shortcut allows the compiler to optimize away the following switch
       // statement for the most common architectures:
    #if defined(_M_IX86) || defined(_M_X64) || defined(__LITTLE_ENDIAN__)
       Architecture = LITTLE_ENDIAN1;
    #elif defined(__BIG_ENDIAN__)
       Architecture = BIG_ENDIAN1;
    #endif
    
       switch (Architecture) {
       case LITTLE_ENDIAN1:
          convert.i[0] =  r << 20;
          convert.i[1] = (r >> 12) | 0x3FF00000;
          return convert.f - 1.0;
       case BIG_ENDIAN1:
          convert.i[1] =  r << 20;
          convert.i[0] = (r >> 12) | 0x3FF00000;
          return convert.f - 1.0;
       case NONIEEE: default: ;
       } 
       // This somewhat slower method works for all architectures, including 
       // non-IEEE floating point representation:
       return (double)r * (1./((double)(uint32)(-1L)+1.));
    }
    
    
    int CRandomMersenne::IRandom(int min, int max) {
       // Output random integer in the interval min <= x <= max
       // Relative error on frequencies < 2^-32
       if (max <= min) {
          if (max == min) return min; else return 0x80000000;
       }
       // Multiply interval with random and truncate
       int r = int((max - min + 1) * Random()) + min; 
       if (r > max) r = max;
       return r;
    }
    
    
    int CRandomMersenne::IRandomX(int min, int max) {
       // Output random integer in the interval min <= x <= max
       // Each output value has exactly the same probability.
       // This is obtained by rejecting certain bit values so that the number
       // of possible bit values is divisible by the interval length
       if (max <= min) {
          if (max == min) return min; else return 0x80000000;
       }
    #ifdef  INT64_DEFINED
       // 64 bit integers available. Use multiply and shift method
       uint32 interval;                    // Length of interval
       uint64 longran;                     // Random bits * interval
       uint32 iran;                        // Longran / 2^32
       uint32 remainder;                   // Longran % 2^32
    
       interval = uint32(max - min + 1);
       if (interval != LastInterval) {
          // Interval length has changed. Must calculate rejection limit
          // Reject when remainder = 2^32 / interval * interval
          // RLimit will be 0 if interval is a power of 2. No rejection then
          RLimit = uint32(((uint64)1 << 32) / interval) * interval - 1;
          LastInterval = interval;
       }
       do { // Rejection loop
          longran  = (uint64)BRandom() * interval;
          iran = (uint32)(longran >> 32);
          remainder = (uint32)longran;
       } while (remainder > RLimit);
       // Convert back to signed and return result
       return (int32)iran + min;
    
    #else
       // 64 bit integers not available. Use modulo method
       uint32 interval;                    // Length of interval
       uint32 bran;                        // Random bits
       uint32 iran;                        // bran / interval
       uint32 remainder;                   // bran % interval
    
       interval = uint32(max - min + 1);
       if (interval != LastInterval) {
          // Interval length has changed. Must calculate rejection limit
          // Reject when iran = 2^32 / interval
          // We can't make 2^32 so we use 2^32-1 and correct afterwards
          RLimit = (uint32)0xFFFFFFFF / interval;
          if ((uint32)0xFFFFFFFF % interval == interval - 1) RLimit++;
       }
       do { // Rejection loop
          bran = BRandom();
          iran = bran / interval;
          remainder = bran % interval;
       } while (iran >= RLimit);
       // Convert back to signed and return result
       return (int32)remainder + min;
    
    #endif
    }
    

    Usage of the above class is quite simple:

    CRandomMersenne generator(<some_seed>);
    generator.random(); // random value [0,1]
    generator.IRandom(a,b); // random value [a,b]
    

    I have tested this many times and it performs better and faster than most random number generators I have seen.

    A lot of times I have relied on the fact that it is deterministic given a seed so you can use it I guess. I will try to find the original source for that code and give the credit to the author.

    EDIT: author of the code above is Agner Fog and on his website there is a whole section for random number generators. All credit for the code goes to him.

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

Sidebar

Related Questions

I need a random number generator that generates various number between n and m,
I need to seed the random number generator in boost (which is loaded from
I'm in need of a C++ (pseudo, i don't care) random number generator that
Like my question, i need to generate random numbers that have identical pairs between
I need help on which random number generator to use simultaneously from multiple threads
I need to generate a lot of random numbers. I've tried using random.random but
I need a random object generator in JavaScript that generates a variety of objects
I use random numbers in several places and usually construct a random number generator
I need to generate a controlled sequence of pseudo-random numbers, given an initial parameter.
I need to use a random number generator very often, and instead of writing

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.