# Random Numbers Lab

Page 1 of 1

## 3 Replies - 2070 Views - Last Post: 10 December 2012 - 11:56 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=303445&amp;s=e613c843182b3c7aaf0e0ea7aeb0c2b2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Shillwil

Reputation: 0
• Posts: 8
• Joined: 30-November 12

# Random Numbers Lab

Posted 10 December 2012 - 10:07 AM

We have a new lab in programming class where we are supposed to use a while loop to generate random numbers until they reach a final sum of 100. I'm not asking for the answers, I was just wondering if anyone could point me in the right direction. Here's the code I have so far. Bare with me here.

```namespace Lab16RandomNums
{
class Program
{
static void Main(string[] args)
{
// Variables for lab
Random rand = new Random();
int sum = 0;
int num = 0;

// Code for lab
while (sum < 100)
{
num = rand.Next(10);
Console.WriteLine("number = " + num);

}
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Random Numbers Lab

### #2 sepp2k

• D.I.C Lover

Reputation: 2262
• Posts: 3,467
• Joined: 21-June 11

## Re: Random Numbers Lab

Posted 10 December 2012 - 10:26 AM

You never change the sum variable, so it will always be less than 100, so the loop will continue forever.

### #3 tlhIn`toq

• Not here as much anymore

Reputation: 6016
• Posts: 13,194
• Joined: 02-June 10

## Re: Random Numbers Lab

Posted 10 December 2012 - 11:53 AM

Basic debuggin would have shown this. Had you put a breakpoint on line 13 you would have observed that the runtime value of sum never changed.

Shillwil: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. That's probably the fault of your school/instructor for not teaching it.

tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated DEC 2012
Spoiler

### #4 Shillwil

Reputation: 0
• Posts: 8
• Joined: 30-November 12

## Re: Random Numbers Lab

Posted 10 December 2012 - 11:56 AM

I got it. We were just introduced to while loops but I figured it out. Just freaked out at first and jumped the gun. My bad.