3 Replies - 1403 Views - Last Post: 10 December 2012 - 11:56 AM Rate Topic: -----

#1 Shillwil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • 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.


TOP most asked:
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



Was This Post Helpful? 0
  • +
  • -

#4 Shillwil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1