Random number, but not showing the same number twice?

Hello everyone! I am making a game with Xamarin Android.
It has 4 rounds, and I want to show 4 pictures at random, but the problem is that it can show pictures twice, or even more often.

I thought this code would fix it, but sadly it didn't.

if (ActivityGame.playing == false)
{
List alreadyGuessed = new List();

                                    Random randomSound = new Random();
                                    int theSound = randomSound.Next(1, 5);
                                    while (alreadyGuessed.Contains(theSound))
                                        theSound = randomSound.Next(1, 5);
                                    alreadyGuessed.Add(theSound);

                                     return theSound;                               
                                }

Anyone got any suggestions on how I make sure that it only shows a number once?

Answers

  • CortezCortez USMember ✭✭✭
    edited November 13
    private List<int> alreadyGuessed = new List<int>();
    private GameType ActivityGame = new GameType();
    private Random randomSound = new Random();
    private theSound;
    
    public int GenerateNewValue()
    {
        if (ActivityGame.playing == false)
        {        
            do
            {       
                    theSound = randomSound.Next(1, 5);
            }
                while (alreadyGuessed.Contains(theSound))
            alreadyGuessed.Add(theSound);
            return theSound;                                        
        }
    }
    
Sign In or Register to comment.