-3

I'm trying to generate random numbers in c#. When I click on the Button the lableBox1 shoud display random numbers in the given rnage and once this is done the labelBox2 should do the same. I numbers should start in increasing order starting from 0 to 1000. I should be able to see the change in the numbers in labelbox one at a time.the number should start from 0 and can stop anywhere within 1000 randomly.

I tried to do it. But when I click on a button I'm getting random numbers in both the labelbox instantly.

My concern is that each lablebox should start generating the series one by one. numbers should start displaying from 0 in a increasing random order and halt anywhere with in 1000;

private void button1_Click(object sender, EventArgs e)
    {

        Random rnd = new Random();
        int c = rnd.Next(100);
        label1.Text = c.ToString();

        Random rng = new Random();
        int d = rng.Next(2875);
        label2.Text = d.ToString();


    }
Praveen
  • 43
  • 3
  • The Next method gives you a single new random number. If you want more, then make a loop or press the button mulitple times. – Emile Jun 16 '15 at 14:38
  • 2
    You should never create `new Random()`'s more then once and only once during the entire lifetime of your program. – Icemanind Jun 16 '15 at 14:43
  • possible duplicate of [Random number generator only generating one random number](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – TaW Jun 16 '15 at 15:50
  • @TaW: while the OP's code certainly suffers from that bug, and while I admit I am unable to understand for sure _what_ the OP is asking for help with, I'm pretty sure he hasn't gotten far enough for that particular problem to be his actual question. I.e. this question isn't a dupe of that other one (though the OP's next question might be :) ). – Peter Duniho Jun 17 '15 at 05:48
  • 1
    It's obvious why both text boxes are updated each time you click the button. You should be able to figure that part out yourself. As for the rest, your question is not clear. Please write a detailed, unambiguous specification describing _exactly_ what you want the code to do. – Peter Duniho Jun 17 '15 at 05:49
  • Did you resolve your problem? – TaW Jun 23 '15 at 05:59

1 Answers1

0

Your code has only 7 lines but still a rather large number of issues.

Let's see:

  • You need to be clear and precise! A dumb machine will have to follow your instructions.. Here seven fellow SO members hace tried to guess what you want to do and more or less failed!

  • You should partition your code into small functions with clear names and little or no dependencies among each other.

  • This will (hopefully) lead to reusable code..

  • You should not code any serious logic into events. Instead only a few checks and calling functional methods is recommened.

You seem to want to generate pairs of random numbers and display them in two Labels.

So I start by deciding whether the generation and the display should go into separate functions. For any but the most trivial problems this should definitely be the case. It is called Model-View separation. But our situation is simple enough to lump them into one function:

void MakeAndShowRandomPair()

We can call it on each click of your button:

private void button1_Click(object sender, EventArgs e)
{
    MakeAndShowRandomPair();
}

Now for the genaration of random numbers. The random number genarator object has two constructors, one with a seed you can choose to control where the sequence should start; this creates a repeatable sequence. The other, without a parameter, uses the current time as its seed. One common mistake is to generate Random object so fast that they are created with the same timestamp as theirs seeds. Those wil always generate the same numbers..

So we genrate it only once at class level:

public Form1()
{
    InitializeComponent();
}

Random rand = new Random();

Now for the function itself. You want the numbers to ascend. The are two ways to achieve that:

  • Either store the last one and repeatedly generate one until the new one is larger than the old one.
  • Or add a random step

I decide to use the latter approach so I need a few variables:

int r1 = 0;
int r2 = 0;
int step1 = 10;
int step2 = 287;

void MakeAndShowRandomPair()
{
   // increase by random steps but at least 1:
   r1 += rand.Next(step1) + 1;
   r2 += rand.Next(step2) + 1;
   // display
   label1.Text = r1.ToString();
   label2.Text = r2.ToString();
}

Note that the variable names are way too short for longer programms! For anything but such a tiny little problem longer and expressive names are obligatory!

Now each click should fill each Label with a larger number.

But maybe some of the commentators were right and you actually want to make this happen automatically?

In that case the button would start (and maybe stop) a Timer and in the Timer.Tick event, guess what - You would call the very same function MakeAndShowRandomPair ! So already we see the benefits of reusable code..

TaW
  • 53,122
  • 8
  • 69
  • 111