1

The Random .NET class provides two methods: NextDouble and Sample. Both document that they return, "A double-precision floating point number greater than or equal to 0.0, and less than 1.0." Sample does document some comments about how as of .NET 2.0, it's not used for several other NextX methods, but I'm not really clear on what implications this has.

So what is the difference between these two methods? What is the intended usage of each? And more specifically, if I'm just after a uniformly distributed value between 0.0 and 1.0, which should I use?

jpmc26
  • 28,463
  • 14
  • 94
  • 146
  • Did you try to use `Sample`? – Magnus Jun 30 '14 at 21:41
  • @Magnus Apparently not. I'm a bit frazzled today. Sorry. – jpmc26 Jun 30 '14 at 21:42
  • This is a very common basic pattern used quite extensively in pretty much any object oriented environment I have come through. It is called [Template Method Pattern](http://en.wikipedia.org/wiki/Template_method_pattern). No, it has nothing to do with C++ templates. – László Papp Dec 27 '14 at 21:12
  • possible duplicate of [Where should we use Template Method - pattern?](http://stackoverflow.com/questions/1553856/where-should-we-use-template-method-pattern) – László Papp Dec 27 '14 at 21:26
  • 1
    @lpapp While this is obviously a pretty bad question (for which I've already apologized; I was having a bad day), I don't see how a specific question about how the `Random` class works could be a duplicate of a general question about when to use a pattern. Even if the `Random` class leverages this pattern, this question clearly isn't about how I should be using that pattern. This is not a duplicate of that question. If you feel this is close-worthy, find a more appropriate reason. – jpmc26 Dec 28 '14 at 14:22
  • @jpmc26: wrong, if you had known about this pattern, you would not really have looked for an answer here. – László Papp Dec 28 '14 at 14:23
  • *facepalm* I asked the question because I didn't notice that `Sample` is `protected`. The pattern had nothing to do with it. Please do not assume to understand a person's thoughts without *asking* them first. – jpmc26 Dec 28 '14 at 14:24
  • The reason why you did not notice it is because you were not aware of the very basic TMP. That is the first thing that should come to mind when someone sees something like that. It is true that the canonical is not only a canonical for this, but other questions, too. – László Papp Dec 28 '14 at 14:24
  • 1
    @lpapp I bow to your omniscience and mind reading powers. Forgive me, a mere insignificant bug compared to your greatness. – jpmc26 Dec 28 '14 at 14:29
  • @lpapp By the way, I understand perfectly well how inheritance is supposed to be used. "Template pattern" is just a fancy name for preparing your class to have particular methods overridden. There's nothing special about it, other than someone slapped a name on it. Template pattern itself also lends itself well to being replaced by composition, which I would lean toward over it. – jpmc26 Dec 28 '14 at 14:46

4 Answers4

6

Sample is the protected method that is called by the public NextDouble method (and certain overloads of Next). It can be overidden to provide a non-default distribution of "random" numbers.

So the only way you can "use" Sample is in a derived class (or reflection). If you don't override Sample then there is no difference between the two.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
4

Sample does document some comments about how as of .NET 2.0, it's not used for several other NextX methods, but I'm not really clear on what implications this has.

It says:

if you derive a class from Random and override the Sample method, the distribution provided by the derived class implementation of the Sample method is not used in calls to the base class implementation of [...] Random.Next() [...]

This is visible from the reference source:

public virtual int Next() {
    return InternalSample();
}

Compared to:

public virtual double NextDouble() {
    return Sample();
}

As Sample() is declared as protected virtual, this means you can create a derived class and override it:

public class VeryRandom : Random
{
    protected override double Sample()
    {
        return 4;
    }
}

Now if you call Next() it'll come from the base class's, whereas NextDouble() will return 4.

As for your question: as long as you don't override Sample() there's no difference between them as NextDouble() calls Sample(), but you can't call Sample() on a Random instance as it is protected and not public.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
3

NextDouble is just the public version of the sample method. No other difference. This is what the documentation says.

The Sample method is protected, which means that it is accessible only within the Random class and its derived classes. To generate a random number between 0 and 1 from a Random instance, call the NextDouble method.

And the documentation of NextDouble clearly states that

This method is the public version of the protected method, Sample.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
1

Looking at the .NET source code, it seems they are doing exactly the same thing, except one is protected and one is public:

  protected virtual double Sample() {
      //Including this division at the end gives us significantly improved
      //random number distribution.
      return (InternalSample()*(1.0/MBIG));
  }

  public virtual double NextDouble() {
    return Sample();
  }
Codeman
  • 12,157
  • 10
  • 53
  • 91