System.Random.Sample 方法

方法描述

返回一个介于 0.0 和 1.0 之间的随机数。

语法定义(C# System.Random.Sample 方法 的用法)

protected virtual double Sample()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Double 大于等于 0.0 并且小于 1.0 的双精度浮点数。

提示和注释

要另外生成一个随机分布或随机数生成器原则,需要从 Random 类派生一个类,并重写 Sample 方法。

重要事项

该 Sample 方法为 protected 时,这意味着它只可由 Random 类及其派生类的内部访问。 要生成一个介于 0 和 1 之间的随机数字(从 Random 实例生成),可调用 NextDouble 方法。

对继承者的说明

从 .NET Framework 2.0 版开始,如果您从 Random 派生类并重写 Sample 方法,则在调用下列方法的基类实现时将不使用由 Sample 方法的派生类实现提供的分布:

Random.NextBytes(Byte[]) 方法。

Random.Next() 方法。

Random.Next(Int32, Int32) 方法,如果(maxValue - minValue 大于 Int32.MaxValue。

而是将使用由 Random 基类提供的统一分布。 此行为提高了 Random 类的总体性能。 若要修改此行为以在派生类中调用 Sample 方法的实现,还必须重写这三个成员的行为。 该示例提供了说明。

System.Random.Sample 方法例子

该分布不同于基类的 Sample 方法所生成的统一分布。

using System;

// This derived class converts the uniformly distributed random 
// numbers generated by base.Sample( ) to another distribution.
public class RandomProportional : Random
{
    // The Sample method generates a distribution proportional to the value 
    // of the random numbers, in the range [0.0, 1.0].
    protected override double Sample( )
    {
        return Math.Sqrt( base.Sample( ) );
    }

    public override int Next()
    {
       return (int) (Sample() * int.MaxValue);
    }   
}

public class RandomSampleDemo  
{
    static void Main( )
    {	
        const int rows = 4, cols = 6;
        const int runCount = 1000000;
        const int distGroupCount = 10;
        const double intGroupSize = 
            ( (double)int.MaxValue + 1.0 ) / (double)distGroupCount;

        RandomProportional randObj = new RandomProportional( );

        int[ ]      intCounts = new int[ distGroupCount ];
        int[ ]      realCounts = new int[ distGroupCount ];

        Console.WriteLine( 
            "\nThe derived RandomProportional class overrides " +
            "the Sample method to \ngenerate random numbers " +
            "in the range [0.0, 1.0]. The distribution \nof " +
            "the numbers is proportional to their numeric values. " +
            "For example, \nnumbers are generated in the " +
            "vicinity of 0.75 with three times the \n" +
            "probability of those generated near 0.25." );
        Console.WriteLine( 
            "\nRandom doubles generated with the NextDouble( ) " +
            "method:\n" );

        // Generate and display [rows * cols] random doubles.
        for( int i = 0; i < rows; i++ )
        {
            for( int j = 0; j < cols; j++ )
                Console.Write( "{0,12:F8}", randObj.NextDouble( ) );
            Console.WriteLine( );
        }

        Console.WriteLine( 
            "\nRandom integers generated with the Next( ) " +
            "method:\n" );

        // Generate and display [rows * cols] random integers.
        for( int i = 0; i < rows; i++ )
        {
            for( int j = 0; j < cols; j++ )
                Console.Write( "{0,12}", randObj.Next( ) );
            Console.WriteLine( );
        }

        Console.WriteLine( 
            "\nTo demonstrate the proportional distribution, " +
            "{0:N0} random \nintegers and doubles are grouped " +
            "into {1} equal value ranges. This \n" +
            "is the count of values in each range:\n",
            runCount, distGroupCount );
        Console.WriteLine( 
            "{0,21}{1,10}{2,20}{3,10}", "Integer Range",
            "Count", "Double Range", "Count" );
        Console.WriteLine( 
            "{0,21}{1,10}{2,20}{3,10}", "-------------",
            "-----", "------------", "-----" );

        // Generate random integers and doubles, and then count 
        // them by group.
        for( int i = 0; i < runCount; i++ )
        {
            intCounts[ (int)( (double)randObj.Next( ) / 
                intGroupSize ) ]++;
            realCounts[ (int)( randObj.NextDouble( ) * 
                (double)distGroupCount ) ]++;
        }

        // Display the count of each group.
        for( int i = 0; i < distGroupCount; i++ )
            Console.WriteLine( 
                "{0,10}-{1,10}{2,10:N0}{3,12:N5}-{4,7:N5}{5,10:N0}",
                (int)( (double)i * intGroupSize ),
                (int)( (double)( i + 1 ) * intGroupSize - 1.0 ),
                intCounts[ i ],
                ( (double)i ) / (double)distGroupCount,
                ( (double)( i + 1 ) ) / (double)distGroupCount,
                realCounts[ i ] );
    }
}

/*
This example of Random.Sample() displays the following output:

   The derived RandomProportional class overrides the Sample method to
   generate random numbers in the range [0.0, 1.0). The distribution
   of the numbers is proportional to the number values. For example,
   numbers are generated in the vicinity of 0.75 with three times the
   probability of those generated near 0.25.

   Random doubles generated with the NextDouble( ) method:

     0.59455719  0.17589882  0.83134398  0.35795862  0.91467727  0.54022658
     0.93716947  0.54817519  0.94685080  0.93705478  0.18582318  0.71272428
     0.77708682  0.95386216  0.70412393  0.86099417  0.08275804  0.79108316
     0.71019941  0.84205103  0.41685082  0.58186880  0.89492302  0.73067715

   Random integers generated with the Next( ) method:

     1570755704  1279192549  1747627711  1705700211  1372759203  1849655615
     2046235980  1210843924  1554274149  1307936697  1480207570  1057595022
      337854215   844109928  2028310798  1386669369  2073517658  1291729809
     1537248240  1454198019  1934863511  1640004334  2032620207   534654791

   To demonstrate the proportional distribution, 1,000,000 random
   integers and doubles are grouped into 10 equal value ranges. This
   is the count of values in each range:

           Integer Range     Count        Double Range     Count
           -------------     -----        ------------     -----
            0- 214748363    10,079     0.00000-0.10000    10,148
    214748364- 429496728    29,835     0.10000-0.20000    29,849
    429496729- 644245093    49,753     0.20000-0.30000    49,948
    644245094- 858993458    70,325     0.30000-0.40000    69,656
    858993459-1073741823    89,906     0.40000-0.50000    90,337
   1073741824-1288490187   109,868     0.50000-0.60000   110,225
   1288490188-1503238552   130,388     0.60000-0.70000   129,986
   1503238553-1717986917   149,231     0.70000-0.80000   150,428
   1717986918-1932735282   170,234     0.80000-0.90000   169,610
   1932735283-2147483647   190,381     0.90000-1.00000   189,813
*/

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。