C# Random Sample

Description

Random Sample returns a random floating-point number between 0.0 and 1.0.

Syntax

Random.Sample has the following syntax.


protected virtual double Sample()

Returns

Random.Sample method returns A double-precision floating point number greater than or equal to 0.0, and less than 1.0.

Example

The following example shows how to use Sample method.


//from  w w  w  . j  a  v  a2s.c o m
using System;

public class RandomProportional : Random
{
    protected override double Sample( )
    {
        return Math.Sqrt( base.Sample( ) );
    }

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

public class RandomSampleDemo  
{
    static void Main( )
    {  
        RandomProportional randObj = new RandomProportional( );

        for( int j = 0; j < 10; j++ ){
           Console.WriteLine(randObj.NextDouble( ) );
        }

        for( int j = 0; j < 10; j++ ){
           Console.Write( "{0,12}", randObj.Next( ) );
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version