0

Is there a way to create a only instance in a class?

At this moment, I'm trying to create only one instance of Random class. Several times I need use it with a certain seed. Is possible to change the seed later?

I'm not sure, I guess to do this is a singleton.

UPDATE: What I need is to have a only one instance of Random class. The idea is to use several times the same seed. But later I need to change the seed, so I'm not sure if the singleton is the best way because I guess change the seed means create a new instance.

Darf Zon
  • 6,268
  • 20
  • 90
  • 149
  • Singleton is the Best! but do explain more of your specific requirements to evaluate other options. – Akhil Jan 27 '12 at 02:10
  • yep its a singleton, [http://stackoverflow.com/questions/5996727/a-simple-implementation-of-a-singleton][1] [1]: http://stackoverflow.com/questions/5996727/a-simple-implementation-of-a-singleton – Anton Jan 27 '12 at 02:12

2 Answers2

7

If you need an instance that is single globally (per domain, per-thread or per-), then Singleton is the right way to go.

See this article from Jon Skeet to see how to do it right.

Since you are trying to create a single instance of the existing class that you can't modify, meaning you can't restrict access to it's constructors, the only way to go would be:

  • wrap this class with a factory that produces single instance
  • ensure that all code uses only your factory to create instances of the this class

If you need a single instance in the context of one class, then I think it's pretty obvious to use something like private static readonly modifiers or control it's instance in any other way since it will be hidden from external word.

Restuta
  • 5,855
  • 33
  • 44
6

In the case you're describing, a singleton is overkill. I generally advise against singletons in any case due to the difficulty in unit testing them.

Since you specified that you'll need to be able to periodically reseed, you'll have to leave off the readonly keyword.

Ex:

public class Foo 
{
    private static Random randGen = new Random();
    public void Foo()
    {
        int i = this.randGen.Next();
    }

    public void Bar()
    {
        int j = this.randGen.Next();
    }

    public void ReseedRandomNumberGenerator(int? seed = null)
    {
        this.randGen = seed.HasValue ? new Random(seed.Value) : new Random();
    }
}
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • This will work purely if you need to create Random non only in the context of the Foo class. – Restuta Jan 27 '12 at 02:18
  • All right. Maybe I'm wrong, readonly means one only instance and to change the seed, we need to create a new instance? – Darf Zon Jan 27 '12 at 02:20
  • @DarfZon You updated your question while I was composing my answer. I'm updating my answer shortly. – Daniel Mann Jan 27 '12 at 02:21
  • 1
    readonly means it can't be modified, static means it will have static reference, only one for the whole application domain. – Restuta Jan 27 '12 at 02:22
  • @DBM thanks for your support DBM, I was not sure if it's right to used a singleton and this is that I was looking for. – Darf Zon Jan 27 '12 at 02:27
  • 1
    It's worth noting that `Random` isn't threadsafe if you're getting values from several threads at once. – porges Jan 27 '12 at 02:39