MediaWiki:Sitenotice:
2024-03-02: The wiki ran out of disk space, so things were not working. This has been resolved by adding another 5GB of quota ;-) Thanks to Tim Lindner for reporting the issues.
2020-05-17: If a page gives you an error about some revision not being found, just EDIT the page and the old page should appear in the editor. If it does, just SAVE that and the page should be restored. OS-9 Al (talk) 12:22, 17 May 2020 (CDT)
Random numbers make easy fun
WELCOME |
---|
Looking for CoCo help? If you are trying to do something with your old Color Computer, read this quick reference. Want to contribute to this wiki? Be sure to read this first. This CoCo wiki project was started on October 29, 2004. --OS-9 Al |
See Recent Changes. | About this site. | Join the E-Mail List or Facebook Group. | Contact me with updates/questions.
This page was last updated on 05/19/2019. Total Pages: 744. Total Files: 994.
Home / Publications / Rainbow / Rainbow 1981 / Rainbow 1981-12 - Random numbers make easy fun
When I first started my data processing classes in college, the subject of computers and Fantasy Role-Playing (FRP) games came up when each student was asked to state his or her goals in learning to program.
Each of us, in turn, explained our ambitions until, in the back of the room, a married couple said they wanted to incorporate use of their computer into a FRP game. The instructor, not being familiar with micro-computers in the home, proclaimed this to be an interesting thought, but he did not see how that would be possible.
One of the unwritten rules of the hobbyist programmer is: DON'T TELL ME IT CAN'T BE DONE! This series of articles, explaining how the Color Computer can be used as an aid to the referee of a FRP game, is dedicated to those people who say "It can't be done."
The Color Computer has a very powerful BASIC, one of the best written and most popular on the personal computer market today. I refer, of course, to the Microsoft BASIC. used in all TRS-80 computers, including the Color Computer.
One of the best functions included in Microsoft BASIC for the Color Computer is the RND (Random) statement. Why is the Color Computer's RND any different from that of other BASICS? I'm glad you asked.
Run the following program:
10 PRINT RND(0)
After running the program, you should see number on the screen with a decimal point to the left. The number will look something like .332515879. Type NEW, press (ENTER) and run this program next:
10 FOR I=1 TO 5:PRINT RND(0); " ";:NEXT I
You should see five different numbers, between 0 and 1. What the RND(0) does is compute a number between 0 and 1. This is a common feature on most personal computers in generating random numbers.
But what good is a random number between 0 and 1? Not much, actually. Not many programs require a random number that is less than 1. In reality, a computer is a glamorized calculator. It can add, subtract, multiply and divide. A decimal number is nothing more than a number in base 10 (l, 10, 100, 1000, etc.). The same is true of numbers less than 1, (.1, .01, .001, etc.).
Run the following program:
10 X=RND(0) 20 PRINT X, 10*X, 100*X
What you should see are three number. with the decimal point in three different places: The first to the left of the number, the second after the first number, and the third after the second number.
Run the program a few more times. Each time there will be a different number with the same decimal format.
In programming, these numbers are referred to as "real" numbers. But even a number like 67.7321187 it is not very useful if we needed only the number 67. Add this line to the program:
30 PRINT (INT X*100)
Now run the program. There should be four numbers on your screen, all the same except for the position of the decimal point, and ... the first two numbers without the decimal point and all numbers to the left of the decimal point. The INT in Line 30 tells the computer to print only the "whole" numbers, or numbers to the left of the decimal point. So now we have a more functional use for the RND statement. But what if you need only a number from 1 to 20, or 1 to 4, or I to 10?
Add this to your program:
40 PRINT INT(X*20)
Run the program a few times. What you should see is that the new number is not the same as the other numbers. Why is this number so different from the others? Let's say the computer chooses .2996951 as the real number. By multiplying by 20, the result will be 5.993902. Adding INT will print only the number to the left of the decimal point. So now, the answer will be 5. Line 40 will create a number from 0 to 19. But wait. We wanted a number from 1 to 20, not 0 to 19! By changing Line 40 to:
40 PRINT INT(X*20)+1
we can generate a number from 1 to 20.
Okay. So now we have any number we want randomly selected by using RND(0). But the Color Computer manual has numbers like RND(l0) and RND(100). What about them? Well, finally we come to the reason why the Color Computer's RND function is different (and in most cases, vastly superior) from other BASIC's.
Even though the Color Computer has the capability of creating a random number using RND(0), we can use the highest number needed as the argument. NEW the program and type this in:
10 PRINT RND(20) 20 FOR I=1 TO 500:NEXT I:GOTOl0
Let the program run for a while. You should see random whole numbers from 1 to 20 printed on your screen. With this function, you can change the number to fit into your FRP game any way your wish. This means you can have a number from 0 to 20, 1 to 20, 0 to l00, or whatever.
One final note. The RND function is not actually a completely randomized number. It is actually what is referred to as a "pseudo-random" number. The computer, in some mysterious way, plucks a number from its memory and computes a number via a routine in its ROM, returning the new number. In this way, the computer appears to be choosing a new random number each time one is called for.
While this month's opening of the series on FRPs is more in the nature of a tutorial on random numbers and their generation, next month's issue of the RAINBOW will explain how I use this function to generate a character, and, also, how PRINT USING can be used to format the screen so that it will be right-justified. Till then, may Odin show favor upon your quests!
Links
See this article as it appeared in the Rainbow Magazine 1981-12 Pag 6, in archive.org