five unique numbers?

i can't figure out how to make these unique:

int num6 = 0;

int[] num = new int[ 6 ];

TextBox[] textbox = {textBox1,textBox2,textBox3,

textBox4,textBox5,textBox6};

if (fiveNumberRadioButton.Checked)

{

for (int i = 0; i < 5; i++)

{

num[ i ] = randomGen.Next(1, 49);

textbox[ i ].Text = num[ i ].ToString();

//if (int.Parse(textbox[i + 1].Text) == num[ i ])

// randomGen.Next();

}

}

the comment is what i tryed, but i dunno. thanks.




Answer this question

five unique numbers?

  • sxzhan


    List<int> num = new List<int>
    while (num.Count < 5)
    {
    int n = randomGen.Next( 1, 49 );
    if (!num.Contains(n))
    num.Add(n);

    }
    TextBox[] textbox = {textBox1,textBox2,textBox3,
    textBox4,textBox5,textBox6};
    for ( int i = 0 ; i < 5 ; i++ )
    textbox[ i ].Text = num[ i ].ToString();




  • Manuel5

    Hi,

    Try something like this:

    int[] num = new int[ 6 ];

    TextBox[] textbox = {textBox1,textBox2,textBox3,

    textBox4,textBox5,textBox6};

    if ( fiveNumberRadioButton.Checked ) {

    for ( int i = 0 ; i < 5 ; i++ ) {

    bool unique = false;

    while ( !unique ) {

    num[ i ] = randomGen.Next( 1, 49 );

    unique = true;

    //verify that new number is unique

    for (int j=0; j<i; j++) {

    unique &= num[ j ] != num[ i ];

    }

    }

    textbox[ i ].Text = num[ i ].ToString();

    }

    }

    Charles


  • five unique numbers?