only return random number when it is unique

Posted by phil on Stack Overflow See other posts from Stack Overflow or by phil
Published on 2011-01-05T15:32:27Z Indexed on 2011/01/05 15:53 UTC
Read the original article Hit count: 152

Filed under:
|
|

My brain is melting today and i cannot think how to do this simple bit of code. numberList is a string of numbers seperated by commas like '2, 34, 10' etc.. when i request a random number i need to check if the string has the number, if it does i want to keep requesting a random number until the random number is definitely not in the string. i cant think what kind of loop i would do to get this to work:

Random r = new Random();

public int RandomPos(int max) {
  int i;
  do {
    i = r.Next(max) + 1;
  }
  while (!numberList.Contains(i.ToString()));

    return i;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about loops