locked
Random Number Generation RRS feed

  • Question

  • I am trying to create two unique random numbers in two different text boxes, here is my code, can you tell me what I need to change to generate two unique numbers instead of the same number in both boxes. namespace RandomReturn1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int rand; private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Random randnum = new Random(); rand = randnum.Next(1, 15); textBox1.Text = rand.ToString(); textBox2.Text = rand.ToString(); } } } Thank you, Brad
    Sunday, July 31, 2011 8:10 PM

Answers

  • Just make sure that the Random instance (reference) in generated on the class level, not in some method, or event:

     

      Random r = new Random();
      private void button1_Click(object sender, EventArgs e)
      {
         int a = r.Next(15);
         int b = r.Next(15);
         textBox1.Text = a.ToString();
         textBox2.Text = b.ToString();
      }
    


     or:

        Random r = new Random();
        private void button1_Click(object sender, EventArgs e)
        {
          textBox1.Text = r.Next(15).ToString();
          textBox2.Text = r.Next(15).ToString();
        }


     


    Mitja
    • Proposed as answer by Pantelis44999 Sunday, July 31, 2011 11:51 PM
    • Marked as answer by Martin_Xie Friday, August 19, 2011 12:27 PM
    Sunday, July 31, 2011 8:26 PM

All replies

  • Just make sure that the Random instance (reference) in generated on the class level, not in some method, or event:

     

      Random r = new Random();
      private void button1_Click(object sender, EventArgs e)
      {
         int a = r.Next(15);
         int b = r.Next(15);
         textBox1.Text = a.ToString();
         textBox2.Text = b.ToString();
      }
    


     or:

        Random r = new Random();
        private void button1_Click(object sender, EventArgs e)
        {
          textBox1.Text = r.Next(15).ToString();
          textBox2.Text = r.Next(15).ToString();
        }


     


    Mitja
    • Proposed as answer by Pantelis44999 Sunday, July 31, 2011 11:51 PM
    • Marked as answer by Martin_Xie Friday, August 19, 2011 12:27 PM
    Sunday, July 31, 2011 8:26 PM
  • namespace RandomReturn1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Random r= new Random(); txt1.Text=r.Next(1,15).ToString(); int temp=r.Next(1,15); while(temp.ToString()!=txt1.Text) { temp=r.Next(1,15); } txt2.Text=temp.ToString(); } } }
    Regards Kumar Gaurav.
    Sunday, July 31, 2011 8:31 PM
  • Thank you! I wanted to use the second one so I could eliminate all the code!!!
    Brad Swinson
    Sunday, July 31, 2011 8:33 PM
  • Thanks all for your help and suggestions.

    Hi Brad,

    Welcome to MSDN Forum.

    If we misunderstood you or you still have any concern about this issue, please kindly elaborate your question.

    Thanks for your active participation.


    Martin Xie [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, August 19, 2011 12:28 PM