The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

# Cube app part 1 • ### Question

• Hi i making an app. I have 2 textBoxes and one random generator. I need to know this: if i press button it will generate 2 numbers, ex. 2 and 5. 5 is bigger so five borders will be visible.

In app is textBlock that says "Winner is :" and next to ":" will be Player 2, cause he has bigger number.

Thank you

Thursday, August 21, 2014 1:14 PM

• Just calling Random.Next twice won't generate unique values: since the calls are not related they can return the same number (in Q Dev's case that will happen 1 in 6 times).

There are a few ways to get around this.

The easiest is to just check if they're the same and then pick again. This will work if you are picking a small number of items from a large set, but could loop for a very long time if you are picking most of the items from a set.

More efficient would be to pick one item, remove it from the set, and then pick the other from the remaining. An easy way to do this is to swap the last item for the picked item and then reduce the logical size of the set:

```            const int numBorders = 8;
int[] borders = new int[numBorders];
for(int i=0;i< numBorders; i++)
{
borders[i] = i;
}

int numAvailable = borders.Length;
// Pick a random number
int cell = rand.Next(numAvailable);
int firstNum = borders[cell];
// Remove it from the set by chopping off the last number
numAvailable--;
// and storing the last one where this number came from
borders[cell] = borders[numAvailable];
// optionally we can save this number at the end
borders[numAvailable] = firstNum;
// then repeat to pick a second number```

As a variation you can shuffle the entire set and then pick them off in order:

```            Random rand = new Random();
// Shuffle the array:
for (int j = borders.Length; j > 0 ; j--)
{
// Pick a random index
int index = rand.Next(j);
// swap the value of that cell and the last one
int temp = borders[index];
borders[index] = borders[j - 1];
borders[j - 1] = temp;
}```

If you can represent your borders as a bit string then you can choose them all in a single call by generating a random number between 0 and 255 and then comparing the bits. The first bit represents the first border, the second bit represents the second border, etc.

I'm not understanding your followup question. What problem are you having with that code? Is it that you don't know how to convert a string to a number? The easiest would be to keep the number in the code-behind and not try to read from the text box, but if you want to you can use the Parse function:

```int num1 = Int16.Parse(textBox1.text);
int num2 = Int16.Parse(textBox2.text);

if(num1 > num2)
{
// some code
}```

--Rob

Monday, August 25, 2014 5:53 PM
• You need an if:

else if (num1 < num2) { ... }

Tuesday, August 26, 2014 3:04 PM

### All replies

• What is your question? Which part of this are you having trouble with?

Friday, August 22, 2014 3:52 AM
• Random generator will generate 2 numbers in same time. One for each textBox. For example 3 and 5. 5 is bigger so 4 borders will hide. (O is visible border and X is invisible border).

O  X   O

X  O  X

O  X   O

Friday, August 22, 2014 9:02 AM
• private void Button_Click_2(object sender, RoutedEventArgs e)
{
String[] strLabel = { "1", "2", "3", "4", "5", "6" };
Random r = new Random();
int iSelect = r.Next(0, 6);
textBox1.Text = strLabel[iSelect];

String[] strLabel2 = { "1", "2", "3", "4", "5", "6" };
Random r2 = new Random();
int iSelect2 = r2.Next(0, 6);
textBox2.Text = strLabel2[iSelect2];
}

Both textBoxes has same number. How can i make for each textBox unique number generated at same time ?

• Edited by Friday, August 22, 2014 2:24 PM
Friday, August 22, 2014 2:06 PM
• ```private void Button_Click_2(object sender, RoutedEventArgs e)
{
Random r = new Random();

String[] strLabel = { "1", "2", "3", "4", "5", "6" };
int iSelect = r.Next(0, 6);
textBox1.Text = strLabel[iSelect];

String[] strLabel2 = { "1", "2", "3", "4", "5", "6" };
int iSelect2 = r.Next(0, 6);
textBox2.Text = strLabel2[iSelect2];
}```

Do not create multiple Randoms in the same call like that. The random values generated from the Random instance depends on a seed. Different seed, different values are resulted. The seed is usually the time the Random class is instantiated. Since this executes so quickly, both r and r2 have the same seed. So they both will result in the same random values. Reusing the same Random instance will prevent this since subsequent calls of Next shouldn't return repeated values everytime.

Improve your Windows Phone UX: get proper templates, styles, and samples at http://modernography.wordpress.com/

Friday, August 22, 2014 4:43 PM
• Thank you.
If in textBox2 (for example) is 5 and in textBox1 is 4 then some borders hide (i know this part of code(borders)).
Friday, August 22, 2014 5:06 PM
• Improve your Windows Phone UX: get proper templates, styles, and samples at http://modernography.wordpress.com/

Monday, August 25, 2014 12:05 PM
• If number in textBox1 is bigger than number in textBox2 something happen

What i mean in code:

if(textBox1.Number > textBox2.Number)

{

some code

}

Monday, August 25, 2014 2:33 PM
• Just calling Random.Next twice won't generate unique values: since the calls are not related they can return the same number (in Q Dev's case that will happen 1 in 6 times).

There are a few ways to get around this.

The easiest is to just check if they're the same and then pick again. This will work if you are picking a small number of items from a large set, but could loop for a very long time if you are picking most of the items from a set.

More efficient would be to pick one item, remove it from the set, and then pick the other from the remaining. An easy way to do this is to swap the last item for the picked item and then reduce the logical size of the set:

```            const int numBorders = 8;
int[] borders = new int[numBorders];
for(int i=0;i< numBorders; i++)
{
borders[i] = i;
}

int numAvailable = borders.Length;
// Pick a random number
int cell = rand.Next(numAvailable);
int firstNum = borders[cell];
// Remove it from the set by chopping off the last number
numAvailable--;
// and storing the last one where this number came from
borders[cell] = borders[numAvailable];
// optionally we can save this number at the end
borders[numAvailable] = firstNum;
// then repeat to pick a second number```

As a variation you can shuffle the entire set and then pick them off in order:

```            Random rand = new Random();
// Shuffle the array:
for (int j = borders.Length; j > 0 ; j--)
{
// Pick a random index
int index = rand.Next(j);
// swap the value of that cell and the last one
int temp = borders[index];
borders[index] = borders[j - 1];
borders[j - 1] = temp;
}```

If you can represent your borders as a bit string then you can choose them all in a single call by generating a random number between 0 and 255 and then comparing the bits. The first bit represents the first border, the second bit represents the second border, etc.

I'm not understanding your followup question. What problem are you having with that code? Is it that you don't know how to convert a string to a number? The easiest would be to keep the number in the code-behind and not try to read from the text box, but if you want to you can use the Parse function:

```int num1 = Int16.Parse(textBox1.text);
int num2 = Int16.Parse(textBox2.text);

if(num1 > num2)
{
// some code
}```

--Rob

Monday, August 25, 2014 5:53 PM
• I wasn't under the impression that the numbers generated had to be unique. If that was the case, then definitely go with Rob's solution. But if the issue was just to have two random numbers (unique or not) and having a tie as a possible outcome, then my solution is sufficient.

Improve your Windows Phone UX: get proper templates, styles, and samples at http://modernography.wordpress.com/

Monday, August 25, 2014 6:23 PM
• I use else (if number in other textBox is bigger) and this: Tuesday, August 26, 2014 2:57 PM
• You need an if:

else if (num1 < num2) { ... }

Tuesday, August 26, 2014 3:04 PM