Department of Energy Argonne National Laboratory Office of Science NEWTON's Homepage NEWTON's Homepage
NEWTON, Ask A Scientist!
NEWTON Home Page NEWTON Teachers Visit Our Archives Ask A Question How To Ask A Question Question of the Week Our Expert Scientists Volunteer at NEWTON! Frequently Asked Questions Referencing NEWTON About NEWTON About Ask A Scientist Education At Argonne Randomize command for BASIC programming
Name: Taylor
Status: N/A
Age: N/A
Location: N/A
Country: N/A
Date: Around 1995


Question:
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!



Replies:
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.

G Bradburn


Click here to return to the Computer Science Archives

NEWTON is an electronic community for Science, Math, and Computer Science K-12 Educators, sponsored and operated by Argonne National Laboratory's Educational Programs, Andrew Skipor, Ph.D., Head of Educational Programs.

For assistance with NEWTON contact a System Operator (help@newton.dep.anl.gov), or at Argonne's Educational Programs

NEWTON AND ASK A SCIENTIST
Educational Programs
Building 360
9700 S. Cass Ave.
Argonne, Illinois
60439-4845, USA
Update: June 2012
Weclome To Newton

Argonne National Laboratory