How to Shuffle a List in C# and VB.NET

To Shuffle a List in C# and VB.NET you can use the snippet below.

Sample C#

public static IList<T> ShuffleIList<T>(IList<T> inputList)
{
	var cryptoServiceProvider = new RNGCryptoServiceProvider();
	var count = inputList.Count;
	while (count > 1)
	{
		var bytes = new byte[1];
		do cryptoServiceProvider.GetBytes(bytes);
		while (!(bytes[0] < count * (Byte.MaxValue / count)));
		var index = (bytes[0] % count);
		count--;
		var input = inputList[index];
		inputList[index] = inputList[count];
		inputList[count] = input;
	}
	return inputList;
}

Sample VB.NET

Public Shared Function ShuffleIList(Of T)(inputList As IList(Of T)) As IList(Of T)
	Dim cryptoServiceProvider = New RNGCryptoServiceProvider()
	Dim count = inputList.Count
	While count > 1
		Dim bytes = New Byte(0) {}
		Do
			cryptoServiceProvider.GetBytes(bytes)
		Loop While Not (bytes(0) < count * ([Byte].MaxValue / count))
		Dim index = (bytes(0) Mod count)
		count -= 1
		Dim input = inputList(index)
		inputList(index) = inputList(count)
		inputList(count) = input
	End While
	Return inputList
End Function

How to get a random Color in C# and VB.NET

To get a random Color in C# and VB.NET you can use the following snippet.

Sample C#

public static System.Drawing.Color RandomColor()
{
	var rand = new Random();
	var r = rand.Next(0, 256);
	Thread.Sleep(1);
	var g = rand.Next(0, 256);
	Thread.Sleep(1);
	var b = rand.Next(0, 256);
	return System.Drawing.Color.FromArgb(255, r, g, b);
}

Sample VB.NET

Public Shared Function RandomColor() As System.Drawing.Color
	Dim rand As var = New Random
	Dim r As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim g As var = rand.Next(0, 256)
	Thread.Sleep(1)
	Dim b As var = rand.Next(0, 256)
	Return System.Drawing.Color.FromArgb(255, r, g, b)
End Function

How to flip a coin in C# or VB.NET

To flip a coin in C# or VB.NET you just need to use this extension method on a instance of random.

Extension Method C#

public static bool FlipCoin(this Random rand)
{
    return rand.Next(2) == 0;
}

Extension Method VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function FlipCoin(rand As Random) As Boolean
	Return rand.Next(2) = 0
End Function

generate random numbers in csharp or vb.net

To generate random numbers in csharp or vb.net you simply need to use the Random class which is located in the namespace System.

c# example

var rand = new Random();
int randInt1 = rand.Next(1, 10);
int randInt2 = rand.Next(10); 

vb.net example

Dim rand = New Random()
Dim randInt1 As Integer = rand.Next(1, 10)
Dim randInt2 As Integer = rand.Next(10)

remember that the first rand.Next statements will return a number between 1 and 9 while the second rand.Next statement will return a number between 0 and 9.

 

Tip

If you do new Random() in a Loop the Random is initialised again and again, your Random function can then possibly return the same number several times.
You should keep a single Random instance and keep using Next on it without reinitialising.