Random.Sample Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Returns a random floating-point number between 0.0 and 1.0.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

abstract Sample : unit -> float
override Sample : unit -> float

Return Value

Type: System.Double

A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0.

To produce a different random distribution or a different random number generator principle, derive a class from the Random class and override the Sample method.


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.

Notes to Inheritors:

Starting with the .NET Framework version 2.0, 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 the following methods:

Instead, the uniform distribution provided by the base Random class is used. This behavior improves the overall performance of the Random class. To modify this behavior to call the implementation of the Sample method in the derived class, you must also override the behavior of these three members. The example provides an illustration.

The following example derives a class from Random and overrides the Sample method to generate a distribution of random numbers. This distribution is different than the uniform distribution generated by the Sample method of the base class.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top