Search code examples
c#.netrandom

Is there a way to grab the actual state of System.Random?


I would like to be able to get the actual state or seed or whatever of System.Random so I can close an app and when the user restarts it, it just "reseeds" it with the stored one and continues like it was never closed.

Is it possible?

Using Jon's idea I came up with this to test it;

static void Main(string[] args)
{
    var obj = new Random();
    IFormatter formatter = new BinaryFormatter();
    Stream stream = new FileStream("c:\\test.txt", FileMode.Create, FileAccess.Write, FileShare.None);
    formatter.Serialize(stream, obj);
    stream.Close();
    for (var i = 0; i < 10; i++)
        Console.WriteLine(obj.Next().ToString());

    Console.WriteLine();

    formatter = new BinaryFormatter();
    stream = new FileStream("c:\\test.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
    obj = (Random)formatter.Deserialize(stream);
    stream.Close();
    for (var i = 0; i < 10; i++)
        Console.WriteLine(obj.Next().ToString());

    Console.Read();
}

Solution

  • It's serializable, so you may find you can just use BinaryFormatter and save the byte array...

    Sample code:

    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    public class Program
    {
        public static void Main(String[] args)
        {
            Random rng = new Random();
            Console.WriteLine("Values before saving...");
            ShowValues(rng);
    
            BinaryFormatter formatter = new BinaryFormatter(); 
            MemoryStream stream = new MemoryStream();
            formatter.Serialize(stream, rng);
    
            Console.WriteLine("Values after saving...");
            ShowValues(rng);
    
            stream.Position = 0; // Rewind ready for reading
            Random restored = (Random) formatter.Deserialize(stream);
    
            Console.WriteLine("Values after restoring...");
            ShowValues(restored);       
        }
    
        static void ShowValues(Random rng)
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(rng.Next(100));
            }
        }
    }
    

    Results on a sample run are promising:

    Values before saving...
    25
    73
    58
    6
    33
    Values after saving...
    71
    7
    87
    3
    77
    Values after restoring...
    71
    7
    87
    3
    77
    

    Admittedly I'm not keen on the built-in serialization, but if this is for a reasonably quick and dirty hack, it should be okay...