Chapter 6 Methods 201 (Web server iis) Consider the following statements:
Chapter 6 Methods 201 Consider the following statements: Random randomObject = new Random(); int randomNumber = randomObject.Next(); The Next method generates a positive int value between zero and the constant Int32.MaxValue(the value 2,147,483,647). If Next produces values at random, every value in this range has an equal chance (or probability) of being chosen when Next is called. Note that values returned by Next are actually pseudo-random numbers a sequence of values produced by a complex mathematical calculation. A seed value is required in this mathematical calculation. When we create our Random object, we use the current time of day as the seed. A particular seed value always produces the same series of random numbers. Programmers commonly use the current time of day as a seed value, since it changes each second and, therefore produces different random-number sequences each time the program executes. The range of values produced directly by Next often is different from the range of values required in a particular application. For example, a program that simulates coin- tossing might require only 0 for heads and 1 for tails. A program that simulates rolling a six-sided die would require random integers in the range 1 6. A video-game program that randomly predicts the next type of spaceship (out of four possibilities) that will fly across the horizon might require random integers in the range 1 4. The one-argument version of method Next returns values in the range from 0 up to (but not including) the value of that argument. For example, value = randomObject.Next( 6 ); produces values from 0 through 5. This is called scaling, because the range of values produced has been scaled down from over two billion to only six. The number 6 is the scaling factor. The two-argument version of method Next allows us to shift and scale the range of numbers. For example, we can use method Next as follows value = randomObject.Next( 1, 7 ); to produce integers in the range from 1 to 6. In this case, we have shifted the numbers to produce a range from 1 up to (but not including) 7. The Windows application of Fig. 6.9 simulates 20 rolls of a six-sided die and shows the integer value of each roll. The dice-rolling simulation begins when the user clicks the Show Ouput button, which invokes the showOutputButton_Click event handler (lines 24 44). The for loop on lines 32 43 repeatedly invokes method Next of class Random to simulate rolling the die. Lines 37 38 append the value rolled to outputLabel s Text property. After every five rolls, line 42 appends a newline character to make the output more readable. 1 // Fig. 6.9: RandomInt.cs 2 // Generating random integer values. 3 using System; 4 using System.Drawing; Fig. 6.9 Random integers in the range 1 6. (Part 1 of 2.)
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.