Does n-th invocation of Random.Next() with the same seed always produce the same number regardless of the .NET version?

StackOverflow https://stackoverflow.com/questions/20885366

  •  23-09-2022
  •  | 
  •  

Question

I am a newbie C# learner and building a system to generate problem sheets from an ordered set of questions. Each problem sheet has 30 question chosen randomly from the set of questions. Each problem sheet is uniquely identified by an integer which is used as the seed value.

As I don't know the internal details of how random generator works, I want to know whether or not the n-th invocation of Random.Next() with the same seed will always produce the same number regardless of the .NET version.

The minimal working example is as follows.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const int seed = 12345;
            Random rnd = new Random(seed);
            for (int x = 0; x < 10; x++)
                Console.WriteLine(rnd.Next());
        }
    }
}
Was it helpful?

Solution

Maybe read the documentation:

Notes to Callers

The implementation of the random number generator in the Random class is not guaranteed to remain the same across major versions of the .NET Framework. As a result, your application code should not assume that the same seed will result in the same pseudo-random sequence in different versions of the .NET Framework.

OTHER TIPS

Yes! you have a possibility to get same random number will be generated if you provide same seed

Random rnd=new Random(5);
            for(int i=0;i<10;i++)
            {
                Console.WriteLine(rnd.Next());
            }

            Console.WriteLine(".....");

            Random rnd1 = new Random(5);

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(rnd1.Next());
            }
            Console.ReadLine();
############### The output
726643700
610783965
564707973
1342984399
995276750
1993667614
314199522
2041397713
1280186417
252243313
.....
726643700
610783965
564707973
1342984399
995276750
1993667614
314199522
2041397713
1280186417
252243313
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top