Radio Shack TRS-80 Model 100 Basic Manual page 156

Basic language lab
Hide thumbs Also See for TRS-80 Model 100:
Table of Contents

Advertisement

Press
(BREAK)
to terminate execution of the program. You can change the range of the
random numbers quite easily. For example, to generate numbers in the range from
zero to 100, simply multiply the RND function by 100. Change the program to
10 PRINT 100*RND(
1)
:
GOTO 10
and execute it. You should see decimal numbers between zero and 100 scroll by. If
you would prefer to have the integers from zero to 100 generated, use the INT
function. Change the program to:
10 PRINT INT(
100*RND(1)
: GOTO 10
and execute it. You should see integers between zero and 100 scroll by. Using these
techniques, you can generate random numbers in any desired range. The qext
experiment illustrates an application of random numbers.
Experiment #7 Simulating a Coin Toss
This experiment will use random numbers to create a simulation of tossing a coin.
A "head" or "tail" can be generated randomly with the use of RND according to the
following scheme:
RND(1 )
between 0 and .5
between .5 and 1
Outcome
Head
Tail
Since the probability of generating a number in the range 0 to 0.5 is equal to the
probability of generating a number in the range 0.5 to 1, this scheme will generate
heads and tails with equal probability.
Use the NEW command to clear memory and enter the following program:
10 CLS
20 A = RND(l)
30 IF A
<
.5 THEN A$="HEAD" ELSE A$="TAIL"
110 PRINT A$,"PRESS ENTER"
50 A$=INKEY$ :
IF A$=""THEN 50
80 GOTO 20
Execute the program.
If
you press
cmEID,
you can generate another coin toss. Continue pressing
CEfITEID
a
few times to see that the coin tosses give the appearance of a random sequence.
Press
(BREAK)
to terminate execution.
Line
18
The display is cleared.
Line 28 A random number is generated and stored in the variable A.
Line 38
If
the value assigned to A is less than .5, then the string "HEAD" is stored
in the string variable A$. Otherwise, the string "TAIL" is stored in A$.
Line 48 The outcome is displayed along with a reminder to press
(ENTER)
to continue.
Line 58 This loop continuously scans the keyboard. When any key is pressed, A$ will
no longer be null and execution resumes on line 60.
150

Advertisement

Table of Contents
loading

Table of Contents