C# में Random.NextDouble () विधि का उपयोग यादृच्छिक फ़्लोटिंग-पॉइंट संख्या को वापस करने के लिए किया जाता है जो 0.0 से अधिक या बराबर है, और 1.0 से कम है।
सिंटैक्स
वाक्य रचना इस प्रकार है -
public virtual double NextDouble ();
उदाहरण
आइए अब एक उदाहरण देखें -
using System;
public class Demo {
public static void Main(){
Random r1 = new Random();
Random r2 = new Random();
Byte[] arr = new Byte[2];
r1.NextBytes(arr);
Console.WriteLine("Random numbers in the byte array...");
for (int i = 0; i < 2; i++)
Console.WriteLine(arr[i]);
Console.WriteLine("\nRandom floating point numbers...");
for (int i = 0; i < 5; i++)
Console.WriteLine(r2.NextDouble());
}
} आउटपुट
यह निम्नलिखित आउटपुट देगा -
Random numbers in the byte array... 124 141 Random floating point numbers... 0.93591266727816 0.36406785872023 0.122396959514542 0.795166163144245 0.954394097884369
उदाहरण
आइए अब एक और उदाहरण देखें -
using System;
public class Demo {
public static void Main(){
int[] val = new int[7];
Random r = new Random();
double d;
for (int i = 0; i 50; i++) {
d = r.NextDouble();
val[(int) Math.Ceiling(d*5)] ++;
}
Console.WriteLine("Random Numbers...");
for (int i = 0; i < 7; i++)
Console.WriteLine(val[i]);
}
} आउटपुट
यह निम्नलिखित आउटपुट देगा -
Random Numbers... 0 13 9 12 8 8 0