C#C
C#2y ago
TheUnquiet

Random Generator method

could someone help me with this method? if the list contains a number we should generate a new random number that is not in the list
c#
public int GeefUniekGetal(int maximum)
{
    const int minimum = 1;
    int result = 0;
    
    if (getallen.Count != maximum)
    {
        if (!getallen.Contains(result))
        {
            result = new Random().Next(minimum, maximum);
            getallen.Add(result);
        } else
        {
            int newResult = new Random().Next(minimum, maximum);
            getallen.Add(newResult);
        }
    } else
    {
        result = 0;
    }
    return result;
}
Was this page helpful?