简体   繁体   English

C#:在单例模式中仅初始化Random()一次

[英]C#: Initialize Random() only once in a singleton pattern

I am trying to initialize only once a random variable, let's say 'rnd'. 我试图只初始化一个随机变量,让我们说'rnd'。 So If from many points of my program it is called, then the random variable will not be initialized again, only first time. 因此,如果从我的程序的许多点调用它,那么随机变量将不会再次初始化,只是第一次。

So I have created a singleton but I do not know how to call only once Random(). 所以我创建了一个单例,但我不知道如何只调用一次Random()。

public sealed class Randomize
{
    private static Randomize instance = null;
    private static readonly object padlock = new object();

    Randomize()
    {
    }
    public static Randomize Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Randomize();
                }
                return instance;
            }
        }
    }
}

Within this class I would like to create a private readonly random variable, 'rnd', which is initialized only once, the first time, to: 在这个类中,我想创建一个私有的只读随机变量'rnd',它只在第一次初始化为:

Random rnd = new Random()

Then, I would like to create a property to read its value, for example: 然后,我想创建一个属性来读取它的值,例如:

        public Random Rnd
        {
            get { return rnd; }
        }

I do not want to allow to change its value because it is assigned first time only, so I only want the property to be readonly (get), not set. 我不想允许更改其值,因为它只是第一次分配,所以我只希望该属性是readonly(get),而不是设置。

So from many points of my program I can do: 所以从我的程序的许多方面我可以做到:

private readonly Random rnd;
Random rnd = Randomize.Instance.Rnd;

But each time I call it using above expression I do not want to initizalize random variable again (by doing new Random()) instead I want to always obtain the same value it was rnd variable was initialized for first time. 但每次我使用上面的表达式调用它时我不想再次启动随机变量(通过执行新的Random())而是我想总是获得与第一次初始化的rnd变量相同的值。

Any ideas on how to do it? 关于如何做的任何想法?

Just use a static constructor, by definition that is gaurenteed to only be called once 只需使用静态构造函数,根据定义,只需调用一次即可

The MSDN Documentation States : MSDN文档状态:

A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. 静态构造函数用于初始化任何静态数据,或执行仅需要执行一次的特定操作。 It is called automatically before the first instance is created or any static members are referenced. 在创建第一个实例或引用任何静态成员之前自动调用它。

public sealed class Randomize
{
    private static Randomize instance = null;
    private static readonly object padlock = new object();

    public Random GetRandom { get { return rnd; } }        

    private static readonly Random rnd;

    static Randomize()
    {
        rnd = new Random();
    }
}

You can access Randomize.Random with the following snippet and it will always return the once-initialized Random rnd field. 您可以使用以下代码段访问Randomize.Random ,它将始终返回一次初始化的Random rnd字段。

public sealed class Randomize {
    private static readonly Random rnd = new Random();
    public static Random Random {
       get {
           return rnd;
       }
    }
}

Read only variables can be assigned in the constructor, so I would go that route. 只读变量可以在构造函数中分配,所以我会去那条路。

public sealed class Randomize {
    /* access level here */ Random rnd { get { return r; } }
    private readonly Random r;

    static Randomize() {
        r = new Random();
    }
}

// or

        private readonly Random r = new Random();
        public Random rnd {
            get {
                return r;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM