C# Generate Random Number

C# Generate Random Number C# Random Number

Do you want to Generate Random unique numbers in C#? Here are C#.net and VB.net examples of Generating Random numbers in Range. The function returns Random number between min and max range. Random is a class in .net whose Next method accepts seed and gives you unique random number. Let's see example.

C# Random Number.
using System;
using System.Threading;

namespace Csharp.RandomNumber_Generator.Example
{
  class Program
  {

    private static readonly Random random = new Random();
    private static readonly object synchLock = new object();

    static void Main(string[] args)
    {

      int minValue = 50;
      int maxValue = 5000;

      for (int i = 0; i <= 10; i++)
      {
        int number = GetRandomNumber(minValue, maxValue);
        Console.WriteLine("Unique Random number within range {0} , {1} is {2} ", 
minValue, maxValue, number); } Console.ReadLine(); } public static int GetRandomNumber(int minValue, int maxValue) { lock (synchLock) { Thread.Sleep(100); return random.Next(minValue, maxValue); } } } }
Output
Unique Random number within range 50 , 5000 is 4215
Unique Random number within range 50 , 5000 is 4818
Unique Random number within range 50 , 5000 is 3337
Unique Random number within range 50 , 5000 is 149
Unique Random number within range 50 , 5000 is 2466
Unique Random number within range 50 , 5000 is 3764
Unique Random number within range 50 , 5000 is 2220
Unique Random number within range 50 , 5000 is 1149
Unique Random number within range 50 , 5000 is 1039
Unique Random number within range 50 , 5000 is 4805
Unique Random number within range 50 , 5000 is 4702
VB.net Random Number 
VB.net Random number
Imports System
Imports System.Threading

Namespace Csharp.RandomNumber_Generator.Example

Class Program

	Private Shared ReadOnly random As New Random()
	Private Shared ReadOnly synchLock As New Object()

	Private Shared Sub Main(args As String())

	Dim minValue As Integer = 50
	Dim maxValue As Integer = 5000

	For i As Integer = 0 To 10
	Dim number As Integer = GetRandomNumber(minValue, maxValue)
	Console.WriteLine("Unique Random number within range {0} , {1} is {2} ", 
minValue, maxValue, number) Next Console.ReadLine() End Sub Public Shared Function GetRandomNumber(minValue As Integer, maxValue As Integer) As Integer SyncLock synchLock Thread.Sleep(100) Return random.[Next](minValue, maxValue) End SyncLock End Function End Class End Namespace
Output
Unique Random number within range 50 , 5000 is 3412
Unique Random number within range 50 , 5000 is 3616
Unique Random number within range 50 , 5000 is 4175
Unique Random number within range 50 , 5000 is 3937
Unique Random number within range 50 , 5000 is 3038
Unique Random number within range 50 , 5000 is 4210
Unique Random number within range 50 , 5000 is 610
Unique Random number within range 50 , 5000 is 2635
Unique Random number within range 50 , 5000 is 1971
Unique Random number within range 50 , 5000 is 1800
Unique Random number within range 50 , 5000 is 2809

Tags:

c# randomC# ref Vs outC# Get Random Item From List (VB.net) Example.C# Recursion ExampleOne dimensional array example C#.net VB.netC# get month number from month textC# foreach loopHow To Declare and access array of string in C#c# arraylistC# DateTime Differencec# string

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.