Thursday, November 1, 2012

Random Numbers

here is the code for a random number

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            //instantiate the program class
            Program p = new Program();
            //call the DisplayRandom method
            p.DisplayRandom();
            Console.ReadKey();
        }

        private int GetRandom()
        {
            //instantiate the Random class
            //rand is the local name of our Random
            //object
            Random rand = new Random();
            //this sets the minimum random number
            //at 1 and the 
            int number = rand.Next(1, 100);
            return number;
        }

        private void DisplayRandom()
        {
            //get fifteen random numbers
            for (int i = 1; i <= 15; i++)
            {
                //the prompt just slows it down enough
                //to make it do different randoms
                Console.WriteLine("Press any key for next number");
                Console.ReadKey();
                Console.WriteLine(GetRandom());
            }
        }
    }
}

No comments:

Post a Comment