Save random numbers generated for later use


This following code will get me a random number:

Random r = new Random();
int srandom = r.Next(2147483647);

I want to save it to a string/int/long or some how so I will be able to use the same number. So for example if I call srandom now I will get random number but after a while it will change. I would like it to stay the same.

You can do the following:

Globals8.GlobalInt8 = Globals8.GlobalInt8.HasValue ? Globals8.GlobalInt8 : srandom;

And you declare the variable GlobalInt8 as a nullable int like this:

public static class Globals8
    private static int? globalInt8 = default(int?);
    public static int? GlobalInt8
         get { return globalInt8;}
         set { globalInt8 = value;}

The same thing can be applied to the long variable. What happens here that you initialize the value to the default value which is similar to null in reference object. After that you check if the variable has value then you ignore it. Else you set its value.

Hope this was useful.