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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:59:34+00:00 2026-05-26T13:59:34+00:00

I have an unsigned int storing a random number. I need to use this

  • 0

I have an unsigned int storing a random number. I need to use this number to generate a series of “random like” numbers and have it generate those exact same numbers if this original random number turns up again.

i.e.

Random number: 123456789

First “random like number”: 3

Second “random like number”: 7

Third “random like number”: 1

Fourth “random like number”: 9

and so on.

Next time that random number comes around, the exact same numbers need to be generated from it.

The non random numbers can be duplicated and/or wrap around (to start again if whatever algorithm is used runs out of numbers, it can start again from the beginning). Just as long as each time, those same numbers are generated.

I realise this is exactly what rand() does (seeding with the random number), however I cannot use rand. So perhaps a more concise question would be how do I replicate a “mini” rand function (it doesn’t have to be anything complicated, just so long as the numbers coming out look somewhat randomised).

Also I cannot use boost (unfortunately).

Any pointers/tips?

  • 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-26T13:59:35+00:00Added an answer on May 26, 2026 at 1:59 pm

    I don’t know exactly why you cannot use rand itself but, if not, then you could just roll your own. Use your integer to seed a number then use the standard Xn+1 = a * Xn + c mod m.

    The Wikipedia page for linear congruential method has some sample values for a, c and m.

    Now LCM isn’t the greatest random number generator in the world but, if you just want numbers that “look somewhat randomised”, it should be sufficient.

    As an example of an LCM algorithm, the following function, based on the Microsoft Visual/Quick C/C++ entry from that linked page above:

    // LCM pseudo-random number generator.
    // Outputs numbers from 0..32767 inclusive.
    // With protection against identical sequences.
    //   due to full 32-bit cycle but only returning 15 bits.
    
    uint32_t myRand (void) {
        const static uint32_t a = 214013U;
        const static uint32_t c = 2531011U;
        // m is, of course, 2^32 since the values will wrap.
    
        static uint32_t seed = 1;
        seed = seed * a + c;
        return (seed >> 16) & 0x7FFF;
    }
    

    has a cycle time of the full range of 32-bit numbers but only returns certain bits of the seed each time, which reduces the appearance of identical sequences.

    If you want a C++ class to do this so that all random number generators are independent of each other (as suggested by TomZ), you can use something like:

    class myRand {
        public:
            myRand ();
            myRand (unsigned int newSeed);
            ~myRand ();
            void setSeed (unsigned int newSeed);
            unsigned int getSeed (void);
            unsigned int next (void);
        private:
            unsigned int seed;
            const static unsigned int a = 214013U;
            const static unsigned int c = 2531011U;
    };
    
    myRand::myRand () { seed = 1; }
    myRand::myRand (unsigned int newSeed) { setSeed (newSeed); }
    myRand::~myRand () { }
    void myRand::setSeed (unsigned int newSeed) { seed = newSeed; }
    unsigned int myRand::getSeed (void) { return seed; }
    unsigned int myRand::next (void) {
        seed = (seed * a + c) & 0xffffffff;
        return (seed >> 16) & 0x7fff;
    }
    

     

    #include <iostream>
    
    int main (void) {
        myRand r (5);
    
        std::cout << r.next() << "\n";          //    54
        std::cout << r.next() << "\n";          // 28693
    
        unsigned int saveSeed = r.getSeed();
        std::cout << r.next() << "\n";          // 12255
        std::cout << r.next() << "\n";          // 24449
    
        r.setSeed (saveSeed);
        std::cout << r.next() << "\n";          // 12255
        std::cout << r.next() << "\n";          // 24449
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen code like this: struct failed_login_res { string errorMsg<>; unsigned int error;
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
I have a class Step derived from std::vector<unsigned int> . I need to overload
I have the following C++ code that tried to generate a random number. The
I have this code: void fill_array (unsigned int *iarray, char *string, int max) {
I have created this code: #include <stdio.h> typedef unsigned int uint; uint in[2]={1,2},out[2]={3,4}; int
I have an unsigned int variable and it can only have the values of
I have the code: unsigned int length = strlen(somestring); I'm compiling with the warning
Suppose I have the following C code. unsigned int u = 1234; int 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.