19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

64 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

■ GENERATING RANDOM NUMBERS: RND<br />

Consider a specific collection of numbers. We say that a process selects a number at r<strong>and</strong>om<br />

from this collection if any number in the collection is just as likely to be selected as any other<br />

<strong>and</strong> the number cannot be predicted in advance. Some examples follow:<br />

Collection Process<br />

1, 2, 3, 4, 5, 6 toss a die<br />

0 or 1, toss a coin: 0 = tails, 1 = heads<br />

–1, 0, 1, . . . , 36 spin a roulette wheel (interpret –1 as 00)<br />

1, 2, . . . , n write numbers on slips of paper, pull one from hat<br />

numbers from 0 to 1 flip the spinner in Figure 2-30<br />

FIGURE 2-30 Spinner to R<strong>and</strong>omly Select a Number Between 0 <strong>and</strong> 1<br />

The function Rnd, which acts like the spinner in Figure 2-30, returns a r<strong>and</strong>om number.<br />

The statement<br />

picBox.Print Rnd<br />

r<strong>and</strong>omly displays a number from 0 up to (but not including) 1. The statement<br />

numVar = Rnd<br />

r<strong>and</strong>omly assigns a number between 0 <strong>and</strong> 1 to the variable numVar. A different number will<br />

be assigned each time Rnd is called in the program, <strong>and</strong> any number greater than or equal to<br />

0 <strong>and</strong> less than 1 is just as likely to be generated as any other. Therefore, although Rnd looks<br />

like a numeric variable, it does not act at all like a variable.<br />

With appropriate scaling, the Rnd function can generate r<strong>and</strong>om numbers from other<br />

collections. The statement<br />

picBox.Print Int(6 * Rnd) + 1;<br />

displays a number from the set 1, 2, 3, 4, 5, 6. Because Rnd always has a value from 0 to 1,<br />

excluding 1, 6 * Rnd has a value from 0 to 6 (excluding 6), <strong>and</strong> Int(6 * Rnd) has one of the<br />

values 0, 1, 2, 3, 4, 5. Adding 1 shifts the resulting number into the desired range.<br />

Suppose the preceding statement is repeated many times. The integers generated should<br />

exhibit no apparent pattern. They should look very much like a sequence of integers obtained<br />

from successively rolling a die. For instance, each of the six integers should appear about<br />

one-sixth of the time <strong>and</strong> be reasonably spread out in the sequence. The longer the sequence,<br />

the more likely this is to occur.<br />

Rnd normally generates the same sequence of numbers each time a program is run.<br />

However, <strong>Visual</strong> <strong>Basic</strong> has another function, R<strong>and</strong>omize, that changes the sequence of numbers<br />

generated by Rnd. This statement will be used in all programs in this text.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!