- Home /
Choose 4 random Images from a Sprite array and load them in premade Image containers without repeat selections?
Hello Unity brotherhood and sisterhood.
I have got a script that loads each Image but I get duplicate results. Can anyone suggest how to solve the duplication ?
I have looked into Fisher Yates Shuffle but I can't get it to work for this scenario.
This is the code that does load a random image on each Image container...
Any suggestions would be great! Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class list : MonoBehaviour
{
// Image containers that get loaded with a random image from the Sprite array
public Image image1;
public Image image2;
public Image image3;
public Image image4;
// Array
public Sprite[] images;
void Start()
{
changeImage();
}
void changeImage()
{
// Load each Image container with a random selection from the Sprite array
int num = UnityEngine.Random.Range(0, images.Length);
image1.sprite = images[num];
int num2 = UnityEngine.Random.Range(0, images.Length);
image2.sprite = images[num2];
int num3 = UnityEngine.Random.Range(0, images.Length);
image3.sprite = images[num3];
int num4 = UnityEngine.Random.Range(0, images.Length);
image4.sprite = images[num4];
}
}
Answer by Hellium · Aug 31, 2020 at 10:19 PM
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.UI;
public class list : MonoBehaviour
{
public List<Image> Images;
public List<Sprite> Sprites;
void Start()
{
AssignSprites(Shuffle(Sprites), Images);
}
void AssignSprites(IList<Sprite> sprites, IList<Image> images)
{
for (int i = 0 ; i < images.Count && i < sprites.Count ; ++i)
images[i].sprite = sprites[i];
}
private static IList<T> Shuffle<T>(IList<T> list)
{
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
IList<T> result = new List<T>(list);
int n = result.Count;
while (n > 1)
{
byte[] box = new byte[1];
do provider.GetBytes(box);
while (!( box[0] < n * ( Byte.MaxValue / n ) ));
int k = (box[0] % n);
n--;
T value = result[k];
result[k] = result[n];
result[n] = value;
}
return result;
}
}
Thank you for your time!
After testing:
I have stored 10 sprites in the list and only first 4 get selected(on every start) instead of full list ?
p.s Those first 4 selected do get randomly placed into the 4 image containers though.
If you have 4 containers, you can't expect to have the 10 sprites displayed. And if you have 4 sprites, you can't expect the 4 to be displayed on the 10 containers without repetition.
I have 4 containers. And the list has 10 sprites.
So the problem is to randomly select 4 out of these 10 to be displayed without repetition.
Your answer
Follow this Question
Related Questions
Trying to assign UI images from an array with scriptable objects not working? 1 Answer
Image appear on click 0 Answers
Get fill amount up and down in a partern 0 Answers
In UI Images how can we radially fill in segments? 1 Answer
Having Trouble with Texture Tiling and offset values. Very specific case. 2 Answers