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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:13:28+00:00 2026-05-12T17:13:28+00:00

I want to repeat a random number sequence generated by a legacy software using

  • 0

I want to repeat a random number sequence generated by a legacy software using the VBMath.Rnd and VBMath.Randomize functions in VB .NET

Reading on the documentation for those functions on MSDN i found that you are supposed to “reset” the generator calling Rnd with a negative value if you want the same seed to give you the same result sequence each time.

But doing some tests… things didn’t work as expected.

The legacy software does something like this at the start of the application on different executions:

float[] rNums = new float[4];

VBMath.Randomize(154341.77394338892);
for (int index = 0; index < 4; index++)
{
    rNums[index] = VBMath.Rnd();
}

And my code does something like this:

VBMath.Rnd(-1);
VBMath.Randomize(154341.77394338892);
for (int index = 0; index < 4; index++)
{
    Console.WriteLine("rNum[" + index + "] " + rNums[index] + " = " + VBMath.Rnd());
}

The results for this test are:

rNum[0] 0,6918146 = 0,2605162
rNum[1] 0,5121228 = 0,4748411
rNum[2] 0,8309224 = 0,8112976
rNum[3] 0,972851  = 0,8011347

The sequence that i want to reproduce in the second code any number of times is the sequence generated from the hard coded initial state of the generator. That means the sequence you would get if you run the first code alone.

I can not change the first code.

Any idea on why the VBMath.Rnd and VBMath.Randomize functions arent working as expected?

Did i miss something?


ANSWER

Thee problem is that since the legacy code doesn’t call Rnd with a negative value, the generator doesn’t clear its state and the call to Rnd gets chained to the previous value of the seed (in this case, the hard-coded value).

To solve the problem and be able to repeat the process all over again without all the problems that would imply “reproducing” the initial state, i cloned the generator code and patched it so i could reproduce the same situation every time depending on a parameter.

I know.. its ugly.. but it solves my problem (Btw i also know that there are some rounding errors and that the generated values are not exact.. they differ in like the last digit or something) but i don’t need exact precision.

The rounding error probably comes from my choice of language for the cloning of the algorithm. If someone could help out on how to get the exact same result (match the rounding errors) that would be nice.

The patched code follows.

public sealed class RndGenerator
{
    static int m_rndSeed = 0x50000;
    // This is the value that the programmer sets the seed at ProjectData object
    // initialization
    const int CONSTANT_INIT_RNDSEED = 0x50000; 

    // Methods
    private static float GetTimer()
    {
        DateTime now = DateTime.Now;
        return (float)(((((60 * now.Hour) + now.Minute) * 60) + now.Second) + (((double)now.Millisecond) / 1000.0));
    }

    public static void Randomize()
    {
        float timer = GetTimer();
        int rndSeed = m_rndSeed;
        int num = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
        num = ((num & 0xffff) ^ (num >> 0x10)) << 8;
        rndSeed = (rndSeed & -16776961) | num;
        m_rndSeed = rndSeed;
    }

    public static void Randomize(double Number)
    {
        Randomize(Number, false);
    }

    public static void Randomize(double Number, bool useHardCodedState)
    {
        int num;

        int rndSeed = 0;
        if (useHardCodedState)
            rndSeed = CONSTANT_INIT_RNDSEED;
        else
            rndSeed = m_rndSeed;

        if (BitConverter.IsLittleEndian)
        {
            num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 4);
        }
        else
        {
            num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
        }
        num = ((num & 0xffff) ^ (num >> 0x10)) << 8;
        rndSeed = (rndSeed & -16776961) | num;
        m_rndSeed = rndSeed;
    }

    public static float Rnd()
    {
        return Rnd(1f);
    }

    public static float Rnd(float Number)
    {
        int rndSeed = m_rndSeed;
        if (Number != 0.0)
        {
            if (Number < 0.0)
            {
                long num3 = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
                num3 &= (long)0xffffffffL;
                rndSeed = (int)((num3 + (num3 >> 0x18)) & 0xffffffL);
            }
            rndSeed = (int)(((rndSeed * 0x43fd43fdL) + 0xc39ec3L) & 0xffffffL);
        }
        m_rndSeed = rndSeed;
        return (((float)rndSeed) / 1.677722E+07f);
    }
}
  • 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-12T17:13:28+00:00Added an answer on May 12, 2026 at 5:13 pm

    ANSWER

    The problem is that since the legacy code doesn’t call Rnd with a negative value, the generator doesn’t clear its state and the call to Rnd gets chained to the previous value of the seed (in this case, the hard-coded value).

    To solve the problem and be able to repeat the process all over again without all the problems that would imply “reproducing” the initial state, i cloned the generator code and patched it so i could reproduce the same situation every time depending on a parameter.

    I know.. its ugly.. but it solves my problem (Btw i also know that there are some rounding errors and that the generated values are not exact.. they differ in like the last digit or something) but i don’t need exact precision.

    The rounding error probably comes from my choice of language for the cloning of the algorithm. If someone could help out on how to get the exact same result (match the rounding errors) that would be nice.

    The patched code follows.

    public sealed class RndGenerator
    {
        static int m_rndSeed = 0x50000;
        // This is the value that the programmer sets the seed at ProjectData object
        // initialization
        const int CONSTANT_INIT_RNDSEED = 0x50000; 
    
        // Methods
        private static float GetTimer()
        {
            DateTime now = DateTime.Now;
            return (float)(((((60 * now.Hour) + now.Minute) * 60) + now.Second) + (((double)now.Millisecond) / 1000.0));
        }
    
        public static void Randomize()
        {
            float timer = GetTimer();
            int rndSeed = m_rndSeed;
            int num = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
            num = ((num & 0xffff) ^ (num >> 0x10)) << 8;
            rndSeed = (rndSeed & -16776961) | num;
            m_rndSeed = rndSeed;
        }
    
        public static void Randomize(double Number)
        {
            Randomize(Number, false);
        }
    
        public static void Randomize(double Number, bool useHardCodedState)
        {
            int num;
    
            int rndSeed = 0;
            if (useHardCodedState)
                rndSeed = CONSTANT_INIT_RNDSEED;
            else
                rndSeed = m_rndSeed;
    
            if (BitConverter.IsLittleEndian)
            {
                num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 4);
            }
            else
            {
                num = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
            }
            num = ((num & 0xffff) ^ (num >> 0x10)) << 8;
            rndSeed = (rndSeed & -16776961) | num;
            m_rndSeed = rndSeed;
        }
    
        public static float Rnd()
        {
            return Rnd(1f);
        }
    
        public static float Rnd(float Number)
        {
            int rndSeed = m_rndSeed;
            if (Number != 0.0)
            {
                if (Number < 0.0)
                {
                    long num3 = BitConverter.ToInt32(BitConverter.GetBytes(Number), 0);
                    num3 &= (long)0xffffffffL;
                    rndSeed = (int)((num3 + (num3 >> 0x18)) & 0xffffffL);
                }
                rndSeed = (int)(((rndSeed * 0x43fd43fdL) + 0xc39ec3L) & 0xffffffL);
            }
            m_rndSeed = rndSeed;
            return (((float)rndSeed) / 1.677722E+07f);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 364k
  • Answers 364k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you're working with the IIS config of your local… May 14, 2026 at 3:35 pm
  • Editorial Team
    Editorial Team added an answer Here is another example: QProcess p; p.start("./ffmpeg", QStringList() << "-t… May 14, 2026 at 3:35 pm
  • Editorial Team
    Editorial Team added an answer No database supports LINQ. LINQ is an abstraction layer on… May 14, 2026 at 3:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.