C# Get Random Item From List (VB.net) Example.

C# Get Random Item From List (VB.net) Example.

C#.net Get Random Items From List Collection. Do you want to get random list items from collection of list? Here is simple example in C#.net and VB.net. We are getting random items from the list for each iteration of the loop. Code is very simple to understand.

C# Random list item example.
using System;
using System.Collections.Generic;
namespace Csharp.Random_List_String.Example
{
  class Program
  {
    static void Main(string[] args)
    {
     
     List ul = new List();
     ul.Add("Reven");
     ul.Add("Merchant");
     ul.Add("Services");
     ul.Add(".Com");
     ul.Add("Author");
     ul.Add("Satalaj");
     ul.Add("More");
     ul.Add("Bird");
     ul.Add("Love");
     ul.Add("Asp");
     ul.Add("Random");
     ul.Add("Select");
     
     List ultemp = new List();
     
     Random r = new Random();
     
     for(int counter=0; counter < 5; counter++)
     {
     
     int item = r.Next(ul.Count);
     
          ultemp.Add(ul[item]);
          
          ul.RemoveAt(item);
          
     Console.WriteLine("Randon item is {0}.", ultemp[counter]);
     
     }
         
    Console.ReadLine();
     
     
    }
  }
}
VB.net Get Random Items From List Collection. VB.net get random items from list.
Imports System
Imports System.Collections.Generic
Namespace VB.Random_List_String.Example
	Class Program
		Private Shared Sub Main(args As String())

		Dim ul As New List(Of String)()
		ul.Add("Reven")
		ul.Add("Merchant")
		ul.Add("Services")
		ul.Add(".Com")
		ul.Add("Author")
		ul.Add("Satalaj")
		ul.Add("More")
		ul.Add("Bird")
		ul.Add("Love")
		ul.Add("Asp")
		ul.Add("Random")
		ul.Add("Select")

		Dim ultemp As New List(Of String)()

		Dim r As New Random()

		For counter As Integer = 0 To 4

			Dim item As Integer = r.[Next](ul.Count)

			ultemp.Add(ul(item))

			ul.RemoveAt(item)


			Console.WriteLine("Randon item is {0}.", ultemp(counter))
		Next

		Console.ReadLine()


		End Sub
	End Class
End Namespace
Output
Randon item is Author.
Randon item is Asp.
Randon item is Services.
Randon item is Random.
Randon item is Reven.

Description: We are using collection of string in terms of List. It's namespace is System.Collections.Generic. Next is a method of Random class instance. It gives random integer value within specified range.

Tags:

c# arraylistlistbox to listboxCheckbox Check all GridviewC# Generate Random NumberC# ArrayList Search using Linq Extension OfType MethodLinq Inner JoinC# Listbox Foreachasp.net add listBox itemsListbox select multipleOne dimensional array example C#.net VB.netJquery Gridview Dropdownlist Selected Change Enable Disable Controls

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.