1 / 22

Lecture Set 9

Lecture Set 9. Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays. Objectives. Understand the concept of random numbers and how to generate random numbers Understand how to declare and manipulate rectangular and higher dimension arrays

wynn
Télécharger la présentation

Lecture Set 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture Set 9 Arrays, Collections and Repetition Part C - Random Numbers Rectangular and Jagged arrays

  2. Objectives • Understand the concept of random numbers and how to generate random numbers • Understand how to declare and manipulate rectangular and higher dimension arrays • Understand how to declare and manipulate jagged arrays • Working with arrays of objects 8/18/2013 1:06 PM

  3. Introduction to Random Numbers • Applications that require random numbers • Gaming and casino applications • Computer simulations • Test data generation • The process of creating a sequence of random numbers is called random number generation 8/18/2013 1:06 PM

  4. Operation of a Random Number Generator • A random number generator is initialized based on a seedvalue • Given the same seed value, the same random number sequence will be generated • Different seed values will generate different random sequences • The time of day (often expressed in milliseconds) is often used to create random seed values 8/18/2013 1:06 PM

  5. The System.Random Class • The System.Random class of the FCL is used to create a random number generator and generate and get random values • We can generate integer and double random values • The constructor of the System.Random class creates the random number generator (an instance of the random class) • Using no constructor arguments, a random seed value is used • The seed value is based on the time of day RandomrndVal= new Random(); • This is the same as RandomrndVal= new Random(DateTime.Now.Millisecond); 8/18/2013 1:06 PM

  6. The Next Method • Without arguments, the Next method gets a positive random Integer value RandomrndValR = new Random;; intrndValI= rndValR.Next(); • With two arguments, a value within a range is returned • The first argument contains the lower bound and the second argument contains the upper bound rndValI= rndValR.Next(1, 10); rndValI= rndValR.Next(1, 6); // Let index be an integer index = rndValR.Next(0, myDictionaryWords.Length - 1); • The second line could be used to generate the value of the roll of a single die. How would you generate a random value representing the roll of two dice? 8/18/2013 1:06 PM

  7. The NextDouble Method • The NextDouble method returns a random value between 0.0 and 1.0 • The value is in the interval [0.0<=value<1.0) • Closed on the left and open on the right • Example: doublerandomDouble; randomDouble= _rndValR.NextDouble() ‘A value such a .4599695143908786 is generated 8/18/2013 1:06 PM

  8. Introduction to Two-dimensional Arrays • A two-dimensional array has rows and columns • Conceptually, it's similar to a grid or table • Two-dimensional arrays have two subscripts instead of one • Declare a two-dimensional array with unspecified dimensions int[,] Table; • Declare a two-dimensional array with 10 rows and 10 columns int [9, 9] Table; 8/18/2013 1:06 PM

  9. Initializing Two-dimensional Arrays • Two-dimensional arrays can be initialized just as one-dimensional arrays can be initialized • The array must not be given an initial size • The initialization list is nested as follows: intSalesArray[,] = { {150, 140, 170, 178}, {155, 148, 182, 190}, {162, 153, 191, 184}, {181, 176, 201, 203} }; 8/18/2013 1:06 PM

  10. Referencing Elements in a Two-dimensional Array • Two-dimensional arrays require two subscripts instead of one • A comma separates the two subscripts • Reference the first row and column in the array named SalesArray int cell; cell = SalesArray[0, 0]; 8/18/2013 1:06 PM

  11. Introduction to Three-dimensional Arrays (optional) • It's possible to create arrays with three dimensions • A three-dimensional array has three subscripts instead of two • Declare a dynamic three-dimensional array intcube[,,]; • Declare a 10 by 10 by 10 array (1000 elements) double cube[10, 10, 10]; 8/18/2013 1:06 PM

  12. Working with Arrays of Objects • Arrays can store object references in addition to storing primary data types • Example to store text box references: textbox [3] textBoxList; textBoxList[0]= txtFirstName; textBoxList[1] = txtLastName; textBoxList[2]= txtAddress; What does this array look like? 8/18/2013 1:06 PM

  13. Memory Allocation to Store an Array of Text Boxes (VB) 8/18/2013 1:06 PM

  14. Another View of 2D (Rectangular) Arrays 8/18/2013 1:06 PM

  15. Operations on a 2-Dimension Array 8/18/2013 1:06 PM

  16. More Operations on a 2-Dimension Array 8/18/2013 1:06 PM

  17. Jagged Arrays (by Example) 8/18/2013 1:06 PM

  18. Jagged Array References (optional) • I am not sure you will have occasion to do this – but maybe • The example is interesting from a pedagogic view • It illustrates memory allocation issues discussed already 8/18/2013 1:06 PM

  19. More Jagged Array (examples – optional) 8/18/2013 1:06 PM

  20. YetANOTHER Example (optional) 8/18/2013 1:06 PM

More Related