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.