using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RandomNumbers2 { class Program { //this program is just a demonstration //of using the Random object. //when declared at class level //the random actually displays //different numbers in the loop below. //first you have to make the Random //object new Random rand = new Random(); static void Main(string[] args) { Program p = new Program(); //call display random p.DisplayRandom(); //call random loop p.RandomLoop(); Console.ReadKey(); } int GetRandom() { //the Next method lets you set //the minimum (1) and the Maximum(100) //bounds for the number to be returned return rand.Next(1, 100); } void DisplayRandom() { //just display the random number returned Console.WriteLine("the random number is {0}" ,GetRandom()); } void RandomLoop() { //declare an array int[] myArray=new int[6]; //loop through the array and add random //numbers for each index for (int i = 0; i < myArray.Length; i++) { //add random to array myArray[i] = GetRandom(); } //In the foreach loop you specifiy //the kind (int) of object stored in the //array or collection and it controls //the number of times you loop //looping once for each object of that type //in the array foreach (int j in myArray) { Console.WriteLine(j); } } } }
Tuesday, October 22, 2013
Using the Random Object
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment