Little Problem

  • Thread starter Thread starter fsd
  • Start date Start date
F

fsd

I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA

Add var1 and var2 together

psuedo code;

switch(var1 + var2)
case 3: three is left
case 4: two is left
case 5: one is left
 
fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.
 
Something like this (pseudo-code):

var1 = Random(3);
var2 = Random(3);
while (var2 == var1)
{
var2 = Random(3);
}
 
fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

if (var1 != var2)
System.out.println ( (6 - var1 - var2) + " is missing" );
else
System.out.println ("Two values are missing, you liar!");
 
try this;

int nArrayPos = -1;
for(int x = 0; x < arrayOfVars.Length; x++)
{
if(var1 != arrayOfVars[x] & var2 != arrayOfVars[x])
{
nArrayPos = x;
}
}
if(nArrayPos != 01)
MessageBox.Show(arrayOfVars[x].ToString() + " is not beeing used");
 
Sorry, the last if should have been;

if(nArrayPos != -1)

Marco Martin said:
try this;

int nArrayPos = -1;
for(int x = 0; x < arrayOfVars.Length; x++)
{
if(var1 != arrayOfVars[x] & var2 != arrayOfVars[x])
{
nArrayPos = x;
}
}
if(nArrayPos != 01)
MessageBox.Show(arrayOfVars[x].ToString() + " is not beeing used");


fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

Think more mathematically. Sum the numbers you have together, and
deduct them from the sum of all three numbers. No loops required.

Brad BARCLAY
 
Unless I'm missing something, this would not work because theoretically you
could get "1" as your answer every time...and that would be stored in each
element of the array list.

Eric

Nicholas Paldino said:
fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
Eric,

That's not what I meant. The array list would be populated with values,
but not randomly. It would be populated with the set of values to select
from (however one comes to those values is irrelevant). Then, a random
number between 0 and the length of the array - 1 is chosen, representing the
index that the value selected is at. That value is placed in another
collection, and the element at that index is removed from the original set.
This reduces the length by one. Even if one is selected every time, it
works because the elements are shifted down one with each removal, providing
a different value.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Eric Johannsen said:
Unless I'm missing something, this would not work because theoretically you
could get "1" as your answer every time...and that would be stored in each
element of the array list.

Eric

message news:#r#[email protected]...
fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
 
fsd said:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA
You could perhaps use the observation that the final number is equal to:

6 - var1 - var2

alex
 
Back
Top