Shuffle Random Images On Button Click Using C#

Many times, we are required to randomize the images, numbers, controls, and positions (Button, Check-Box, Radio-Button etc.). Therefore, we use the Random() function. This blog guides you through the process of shuffling random images on a button-click.

Often, we need to randomize the images, numbers, controls, and positions (Button, Check-Box, Radio-Button etc.). For that, we use the Random() function in C#.

The Random.Next() method is also available in the Random class which returns random numbers.

Shuffle Random Images On Button Click Using C# 

This Range() method requires parameters such as minimum and maximum number to set the range. In this example, I have taken the numbers (0, 4) in the range method, which contains the number - 0,1,2,3, i.e., a total of 4 numbers.

Whenever the Random.Next() method is executed on the above range, it will return the output in a random list, like 2,0,1,3 or 3,2,1,0 or 1,3,0,2 or 0,2,1,3 etc.

  1. var randomNumbers = Enumerable.Range(0, 4).OrderBy(x => rnd.Next()).Take(4).ToList();  

On every click of “Shuffle” button, the above line of code will return the list of 4 numbers. The sequence gets changed everytime. By using this number, we can set the position of images.

As soon as the position of images has changed, it behaves like the images are shuffled.

We can apply the same for randomizing Controls, Pages, Tabs, Numbers, etc.

On click of the“Shuffle” button, it shuffles the pictures randomly.

Step 1

Add the picture boxes on your form as per the requirement. In this example, I have taken 4 picture boxes.

Step 2

Add a button to shuffle the images.

Step 3

Double-click on the button to open the code behind of the form.

Step 4

Create a list, first for the images and second for the PictureBox.

Step 5

In the Form, the default Constructor initializes the images and creates a DirectoryInfo. Then, it sets the appropriate path and adds picture boxes to the list of picture boxes.

Step 6

Create one function which calls the Button-Click event. In this function, set the Enumerable.Range() like below example. I have taken 4 images; therefore I set 4.
  1. Enumerable.Range(0, 4).OrderBy(x => rnd.Next()).Take(4).ToList(); 

Source Code

  1. using System;    
  2. using System.Threading;    
  3. using System.IO;    
  4. using System.Collections.Generic;    
  5. using System.ComponentModel;    
  6. using System.Data;    
  7. using System.Drawing;    
  8. using System.Drawing.Imaging;    
  9. using System.Drawing.Drawing2D;    
  10. using System.Linq;    
  11. using System.Text;    
  12. using System.Threading.Tasks;    
  13. using System.Windows.Forms;    
  14. namespace ShuffleDemo  
  15. {  
  16.     public partial classForm1: Form   
  17.     {  
  18.         List<Image> images;  
  19.         List<PictureBox> pic = new List<PictureBox>();  
  20.         public Form1()  
  21.         {  
  22.             InitializeComponent();  
  23.             images = new List<Image>();  
  24.             DirectoryInfo di = new DirectoryInfo(@ "F:\Praveen\Images"); // give a path    
  25.             FileInfo[] finfos = di.GetFiles("*.jpg", SearchOption.AllDirectories);  
  26.             foreach (FileInfo fi in finfos)  
  27.                 images.Add(Image.FromFile(fi.FullName));  
  28.             //add picture boxes    
  29.             pic.Add(pictureBox1);  
  30.             pic.Add(pictureBox2);  
  31.             pic.Add(pictureBox3);  
  32.             pic.Add(pictureBox4);  
  33.         }  
  34.         Random random = new Random();  
  35.         private void Form1_Load(object sender, EventArgs e) { }  
  36.         public void shuffleImages()  
  37.         {  
  38.             var rnd = new Random();  
  39.             var randomNumbers = Enumerable.Range(0, 4).OrderBy(x => rnd.Next()).Take(4).ToList();  
  40.             int[] n = new int[4];  
  41.             List<int> a = newList<int>();  
  42.             foreach (int item in randomNumbers)  
  43.             {  
  44.                 a.Add(item);  
  45.             }  
  46.             for (int i = 0; i < pic.Count; i++)  
  47.             {  
  48.                 n[i] = a[i];  
  49.                 pic[i].Image = images[n[i]];  
  50.             }  
  51.         }  
  52.         private void btnShuffle_Click(object sender, EventArgs e)  
  53.         {  
  54.             shuffleImages();  
  55.         }  
  56.     }    
  57. }