C# | Random.NextDouble() Method
Last Updated :
01 May, 2019
The NextDouble() Method of System.Random class in C# is used to return a random floating-point number which is greater than or equal to 0.0, and less than 1.0.
Syntax:
public virtual double NextDouble();
Return Value: This method returns a double-precision floating point number which is greater than or equal to 0.0, and less than 1.0.
Below program illustrates the use of NextDouble() Method:
Example 1:
using System;
class GFG {
public static void Main()
{
Random rand = new Random();
Console.WriteLine( "Printing 10 random floating point numbers" );
for ( int i = 0; i < 10; i++)
Console.WriteLine( "{0} -> {1}" , i, rand.NextDouble());
}
}
|
Output:
Printing 10 random floating point numbers
0 -> 0.0227202852362396
1 -> 0.624568469647583
2 -> 0.0145442797870116
3 -> 0.646489209330869
4 -> 0.967497945748036
5 -> 0.839329582098559
6 -> 0.873648912121378
7 -> 0.16200648022909
8 -> 0.66018275761054
9 -> 0.0837694853934317
Example 2:
using System;
class GFG {
public static void Main()
{
Random rand = new Random();
double [] a = new double [10];
for ( int i = 0; i < 10; i++)
a[i] = rand.NextDouble();
Console.WriteLine( "Printing 10 random " +
"floating point numbers" );
for ( int i = 0; i < 10; i++)
Console.WriteLine( "{0} -> {1}" , i, a[i]);
}
}
|
Output:
Printing 10 random floating point numbers
0 -> 0.853536825558886
1 -> 0.741455778359182
2 -> 0.496043408986201
3 -> 0.0975164361752181
4 -> 0.120282317567748
5 -> 0.57163705703413
6 -> 0.749181974562435
7 -> 0.684014179596684
8 -> 0.691246760865323
9 -> 0.888019556127498
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...