- Trending Categories
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.Next() Method in C#
The Random.Next() method in C# is used to return a non-negative random integer.
Syntax
The syntax is as follows −
public virtual int Next (); public virtual int Next (int maxVal);
Above, the maxVal parameter is the exclusive upper bound of the random number to be generated.
Example
Let us now see an example −
using System; public class Demo { public static void Main(){ Random r = new Random(); Console.WriteLine("Random numbers....."); for (int i = 1; i <= 5; i++) Console.WriteLine(r.Next()); } }
Output
This will produce the following output −
Random numbers..... 1014639030 1510161246 1783253715 487417801 249480649
Example
Let us now see another example −
using System; public class Demo { public static void Main(){ Random r = new Random(); Random r2 = new Random(); Console.WriteLine("Random numbers....."); for (int i = 1; i <= 5; i++) Console.WriteLine(r.Next()); Console.WriteLine("
Random numbers from 1 to 10....."); for (int i = 1; i <= 5; i++) Console.WriteLine(r2.Next(10)); } }
Output
This will produce the following output −
Random numbers..... 613432308 1705125884 1787561614 1243383842 2016323534 Random numbers from 1 to 10..... 2 7 8 5 9
Advertisements