picturebox image randomisation C#

11,177

Solution 1

Create an array of bools equal to the size of the pictures array

bool[] usedPictures = new bool[pictures.Length];

Set the values of this array to false. Now determine your random number, and test if that element is used or not, something like:

int iCount = 0;
Random random = new Random();
while (iCount < pictures.Length)
{
    int attempt = random.Next(0, pictures.Length);

    //Ensures you will only use an available picture
    if (usedPictures[attempt] == false)
    {            
        picBox[attempt].Image= pictures[iCount];
        doorUsed[attempt] = true;
        iCount++;
    }
}

Solution 2

Simply fill the array and use a shuffle algorithm.

Perhaps implement as an extension method:

namespace ExtensionMethods
{
    public static class Extensions
    {
        static Random rng = new Random();

        public static void shuffle<T>(this T[] array)
        {
            // i is the number of items remaining to be shuffled.
            for (int i = array.Length; i > 1; i--)
            {
                // Pick a random element to swap with the i-th element.
                int j = rng.Next(i);  // 0 <= j <= i-1 (0-based array)
                // Swap array elements.
                T tmp = array[j];
                array[j] = array[i - 1];
                array[i - 1] = tmp;
            }
        }

    }
}

Calling sample:

using ExtensionMethods;

namespace ConsoleApplication
{

    static class Program
    {
        static void Main()
        {
            int[] array = new int[] {1,2,3,4,5,6,7,8,9};

            array.Shuffle();
        }
    }
}
Share:
11,177
cheesebunz
Author by

cheesebunz

:D

Updated on June 04, 2022

Comments

  • cheesebunz
    cheesebunz almost 2 years

    i am working on a puzzle slider program and trying to randomize images inside of pictureboxes. I did some research on the internet can't find any examples i could work on. These are my code:

            Random r = new Random();
    
            PictureBox[] picBox = new PictureBox[9];
            picBox[0] = new PictureBox();
            picBox[1] = new PictureBox();
            picBox[2] = new PictureBox();
            picBox[3] = new PictureBox();
            picBox[4] = new PictureBox();
            picBox[5] = new PictureBox();
            picBox[6] = new PictureBox();
            picBox[7] = new PictureBox();
            picBox[8] = new PictureBox();
    

    i have bitmap array too:

            Bitmap[] pictures = new Bitmap[9];
            pictures[0] = new Bitmap(@"1.1Bright.jpg");
            pictures[1] = new Bitmap(@"1.2Bright.jpg");
            pictures[2] = new Bitmap(@"1.3Bright.jpg");
            pictures[3] = new Bitmap(@"2.1Bright.jpg");
            pictures[4] = new Bitmap(@"2.2Bright.jpg");
            pictures[5] = new Bitmap(@"2.3Bright.jpg");
            pictures[6] = new Bitmap(@"3.1Bright.jpg");
            pictures[7] = new Bitmap(@"3.2Bright.jpg");
            pictures[8] = new Bitmap(@"3.3Dark.jpg");
    

    i tried a few ways but i don't know how to set random pictures[] into the picBox[]:

            for(int i=0; i<=8;i++)
            {
                picBox[i].Image= pictures[r.Next(0,9)];
            }
    

    the problem here is that some pictureboxes e.g picBox[1] and picBox[6] are repeated pictures. How do i make them non repeats? Examples are greatly appreciated thanks.

  • cheesebunz
    cheesebunz almost 14 years
    Wow thanks looks like i have to study the algorithm. Would take some time though. In the meanwhile , are there any simple codings for my rather weak maths brain to start with :X
  • cheesebunz
    cheesebunz almost 14 years
    Hey robb THANK YOU THANK YOU THANK YOU THANK YOU ! it 100% works and all my images are shuffeled and random-ed! 10/10 thumbs up to u :):)
  • cheesebunz
    cheesebunz almost 14 years
    This is rather complex to me lol but it does work, i get the logic
  • ICR
    ICR almost 14 years
    This works fine for such a small number of items to shuffle (9), but you should be aware this solution doesn't scale. If you need to shuffle a larger number of items in the future, it's worth looking into better shuffling algorithms.
  • ICR
    ICR almost 14 years
    The intuition is that you go through the collection of items, swapping each item with another random item. In order to ensure the shuffle is properly random, with no bias (i.e. no combination is more likely than another) you need to make sure you only swap with items that are after the current item (or before, as most implementations run through the array backwards to simplify the maths). Without going into the maths, this is just something you have to trust people on.
  • Robb
    Robb almost 14 years
    @ICR, definitely. I figured that since his puzzle was so static that this solution would do the job in a simple fashion which was really all he wanted.
  • cheesebunz
    cheesebunz almost 14 years
    Yes, thanks all of you. I'll look into the better algorithm when develop a program with more items. Thank you all, cheer :)