Question from noob. There are 10 images and 5 PictureBox elements. Clicking the button loads 5 random images into these windows, how to prevent them from repeating?
3 answers
Let there be a list of 10 images. We select one random from it, display it in the first window and remove it from the list. Select the second random image from the remaining list (already without the first one), display it in the second window and delete it again from the list and repeat further.
Minus: the images are deleted from the list, so in order to repeat the process you will have to keep the original list and copy it to the working one from which you already have to delete. The speed will fall on the copying of lists, but not very noticeable, since the links to the images will be copied.
Plus: ease of implementation and reliability of the algorithm (just do not repeat the image).
- Much better: set up a list of 10 numbers. Perform the specified operation on this list, and then simply display the elements of the list of images with the selected numbers. - kirelagin 1:51 pm
- At first I also thought about numbers, but we won’t win anything from it, because there would still be 2 lists (with images and numbers), and the memory size would be the same (almost, the object handler is equal to int in Sharp?) . - IAZ
- Yes, in lucidity, we will not lose much - the solution through the numbers is still very obvious, and more universal (for example, if we choose not from the list with file names, but with the files we’ve already downloaded, delete \ download files if several times It will be necessary to display five pictures). Yes, and it seems that creating / deleting ints works much faster than objects of any kind. - kirelagin pm
- @kirelagin create objects do not need, just copy the links - IAZ
- The main thing when deleting from the list is that the link remains somewhere;). - kirelagin
As an option to store images (or paths to them) in the list and store their number. It is called by a random with the current quantity, entered into the first box, removed from the list, the quantity decreases, etc.
What is the difficulty? Each time you get the name of a random image, check whether it has already been inserted. If so, get another random image. Do this until we get something that has not yet existed.