 |
Ask A Scientist©
Computer Science Archive
|
 |
Randomize command for BASIC programming
Index Key: COM047
Author: taylor x
Subject: Randomize command for BASIC programming.
What is the command in BASIC programming to have the computer make a random
selection? I am doing a program and it is driving me bonkers!
Response #: 1 of 1
Author: gregory r bradburn
Assign sequential integers to represent the possible selections. Call the
random number generation function (perhaps RAN, RANDOM or RND??) to get a
real number from 0.0 to 1.0 (If your lucky your random number generator will
return integers in a selected range. If so, you are done!) Now you just
need to devise a way to "map" the number returned from your random number
generator to your selections. If you have assigned numbers to your selec-
tions starting with 1 then multiply the random number by the number of
selections and add 1.0. Finally, truncate the resulting real number. The
integer part will (randomly) point to one of your selections.
NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators.
Argonne National Laboratory, Division of Educational Programs, Harold Myron, Ph.D., Division Director.