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

The Archive Base Latest Questions

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

I would like to be able to get the actual state or seed or

  • 0

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();
}
  • 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-26T21:03:34+00:00Added an answer on May 26, 2026 at 9:03 pm

    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…

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

Sidebar

Related Questions

I would like to be able to get a list of all possible files
I would like to be able to get the name of a variable as
I would like to be able to get as many properties from a button
I would like to be able to get a list of parameters in the
I have a TcxScheduler where I would like to be able to get the
i would like an index action to be able to take get and post
Would like to be able to set colors of headings and such, different font
I would like to be able to output the name of a variable, along
I would like to be able to print in the logs a message for
I would like to be able to show a personal message to abusers of

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.