Math problem

  • Thread starter Thread starter Sebastián
  • Start date Start date
S

Sebastián

Hi all,
I have to randomlly pick numbers, but not any any number, it has to be from
a predefined set of posible values. Is there an "easy" way to do that????

TIA,
Sebastián
 
Hi all,
I have to randomlly pick numbers, but not any any number, it has to be
from
a predefined set of posible values. Is there an "easy" way to do that????

Do this:
int[] possibleValues=new int[] { 4,33,67,42,129 };
Random rnd=new Random();
int
randomValueFromPredefinedSet=possibleValues[rnd.Next(0,possibleValues.Length)];

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
randomValueFromPredefinedSet=possibleValues[rnd.Next(0,possibleValues.Length)];
Ouch. A tiny bug above. I forgot to subtract 1 from the
possibleValues.Length. I noticed that you had posted this question to the C#
group as well after posting the reply so I reckon you've gotten a good
answer.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Back
Top