0

Im just learning C# at uni and seem to be struggling to get this random number to out put in a textbox. I havnt learnt a language before this so apologies if its a simple question.

I want to create 5 random numbers, between two different numbers specified by the user. Then print the 5 numbers generated into a textbox to display them.

private void button1_Click(object sender, EventArgs e) {

        int firstnum = Int32.Parse(txtboxnum.Text);
        int secondnum = Int32.Parse(txtboxnum2.Text);


        Random random = new Random();
        int randomNumber = random.Next(firstnum, secondnum);

        Int32 loop = 0;
        do
        {
            loop++;
        }
        while (loop > 5);

        string myString = randomNumber.ToString();

       string  txtboxanswer = myString;

I would be much appreciative of any suggestions where im going wrong. As i just cant seem to get the answer to appear in my textbox. I know i must be close. I hope :)

Thanks for all help in advance.

2
  • you need all the 5 values in same text box altogether at a time? Commented Oct 24, 2012 at 18:58
  • THis seems like homework, maybe it should have that tag? Commented Oct 24, 2012 at 19:27

6 Answers 6

1

by the way, this is how I'd do it

        int firstnum = 0;
        int secondnum = 1;
        Random random = new Random();

        int[] randomnums = new int[5];

        for (int i = 0; i < randomnums.Length; i++)
        {
            randomnums[i] = random.Next(firstnum, secondnum);
        }

and then you can do something like

string myString = randomnums[1].ToString() 

to get the string representation of a particular number.

Sign up to request clarification or add additional context in comments.

Comments

1

You are close enough. With minimum editing of your code, it should be pretty much like this

int firstnum = Int32.Parse(txtboxnum.Text);
    int secondnum = Int32.Parse(txtboxnum2.Text);


    Random random = new Random();


    Int32 loop = 0;
    do
    {
       int randomNumber = random.Next(firstnum, secondnum);
       string myString = randomNumber.ToString();
       TextBox t= new TextBox();
       t.Text=myString; 
       t.Left=0;t.Top=loop * 20;
       this.Controls.Add(t);

       loop++;
    }
    while (loop < 5);

For more on adding controls dynamically check this

2 Comments

It should be while(loop < 5);
Also you should put the code for generating new random number inside the loop. For each iteration there should be new generated random number.
0

Simply, you did not set the Text property of the TextBox to the result.

txtBoxName.Text= myString

3 Comments

string txtboxanswer.Text = myString; This gives me a red line under the txtboxanswer.Text am i missing something else?
got it. take the string out at the start. Thanks
@user1735367 I assume that you have a TextBox called txtBoxName
0

you are on the right track, I think the only thing you are missing is to assign the string to a textbox. Just place another textbox (let's say answertxtbox) and write in the end of your loop the following:

answertxtbox.Text += txtboxanswer + ", ";

Comments

0

Try this :

Random rand = new Random();
for (int i = 0; i < 5; i++)
{
    int a = rand.Next(Min, Max);
    textBox1.Text =  textBox1.Text + string.Format(" {0} ", a.ToString());
}

1 Comment

What's the use of for loop here? You are overwriting last value after every iteration.
0

Just because everyone seems to be answering...

 int firstnum = Int32.Parse(txtboxnum.Text);
 int secondnum = Int32.Parse(txtboxnum2.Text);
 Random random = new Random();

 List<int> results = new List<int>();
 for (int i =0; i < 5; i++)
 {
     results.Add(random.Next(firstnum, secondnum));
 }

 answertxtbox.Text = String.Join(",", Array.ConvertAll<int, String>(results.ToArray(), Convert.ToString));

Note: I wouldn't actually do it this way but it does illustrate that there are many, many ways of solving the same problem.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.