Help Needed Initialising Class

  • Thread starter Thread starter Mick Walker
  • Start date Start date
M

Mick Walker

I have the following class:

class Axis
{
private int _num1;
private int _num2;
private int _num3;

public int Num1 {
get { return _num1; }
set { _num1 = value; }
}

public int Num2 {
get { return _num2; }
set { _num2 = value; }
}

public int Num3 {
get { return _num3; }
set { _num3 = value; }
}

public Lotto() {

}

public Lotto(int[] ExcludedNumbers) {
// Initialise variable to random numbers not in the above array
}
}

What I would like, to be able to do is initialise this class setting
Num1, Num2, Num3 to random values (they must differ from each other).
However when I initialise it, passing an array of integer I would like
Num1, Num2, Num3 to be initialised to random values, but if the value
exists in the ExcludedNumbers array, a different random value should be
used, as before, these values should all differ from each other.

If someone could help that would be great, I'm not nessesarily looking
for a code soloution, but just some help on they way.

Kind Regards
Mick Walker
 
How about something along the lines of below; note that it isn't fully
thread-safe, since I haven't sync'd access to the shared random class:

public Lotto(int[] ExcludedNumbers)
{
Num1 = GetRand(ExcludedNumbers);
Num2 = GetRand(ExcludedNumbers, Num1);
Num3 = GetRand(ExcludedNumbers, Num1, Num2);

}
public static int GetRand(int[] primaryExclusions, params
int[] secondaryExclusions)
{
do // watch out for saturation!!! (i.e. impossible to find
another number)
{
int val = rand.Next(); // range...
if (Array.IndexOf(primaryExclusions, val) < 0
&& Array.IndexOf(secondaryExclusions, val) < 0)
{
return val;
}
} while (true);
}

static Random rand = new Random();


Marc
 

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

Back
Top