- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Random.NextDouble() Method in C#
The Random.NextDouble() method in C# is used to return a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
Syntax
The syntax is as follows −
public virtual double NextDouble ();
Example
Let us now see an example −
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("
Random floating point numbers..."); for (int i = 0; i < 5; i++) Console.WriteLine(r2.NextDouble()); } }
Output
This will produce the following output −
Random numbers in the byte array... 124 141 Random floating point numbers... 0.93591266727816 0.36406785872023 0.122396959514542 0.795166163144245 0.954394097884369
Example
Let us now see another example −
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]); } }
Output
This will produce the following output −
Random Numbers... 0 13 9 12 8 8 0
Advertisements
To Continue Learning Please Login
Login with Google