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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Randomlly pick number 3
VBA selection input question 4
Math 9
Office outlook 2007 issue 2
Windows Explorer Add-In 2
math formulas in .NET 5
Excel How To Convert Decimal To Inches 2
Can't access file system 1

Back
Top