user1735367
user1735367

Reputation: 119

I want to create 5 random numbers between 2 numbers and display in my textbox

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.

Upvotes: 0

Views: 2471

Answers (6)

Arjun Shetty
Arjun Shetty

Reputation: 1585

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

Upvotes: 1

RobH
RobH

Reputation: 3612

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.

Upvotes: 0

Ali Vojdanian
Ali Vojdanian

Reputation: 2111

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());
}

Upvotes: 0

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.

Upvotes: 1

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

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

txtBoxName.Text= myString

Upvotes: 0

CloneXpert
CloneXpert

Reputation: 246

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 + ", ";

Upvotes: 0

Related Questions