C# Generate 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**
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