home *** CD-ROM | disk | FTP | other *** search
/ Big Blue Disk 4 / bbd04new.zip / BITS&PCS.TXT next >
Text File  |  1986-12-01  |  6KB  |  106 lines

  1. ^C^1BITS 'N PC's
  2. ^CBy Daniel Tobias
  3.  
  4.  
  5. A collection of Hints, Tips and Bug Reports for all programmers and intermediate
  6. to advanced users.
  7.  
  8.  
  9. ^CA BIT OF RANDOMNESS
  10.  
  11. It's time for something completely random.  (As opposed to Ranndom, the title 
  12. used by Richard and Lavona Rann for their news column elsewhere in this issue.)
  13. We will now talk about generating random numbers in your programs.
  14.  
  15. There are many reasons why you might want to have random numbers in your 
  16. program.  For instance, you might be writing a card game program which must 
  17. deal hands to the players.  Or, you may wish to create a simulation which 
  18. generates events at unpredictable times to test algorithms which are intended 
  19. to deal with real-world events.
  20.  
  21. For these reasons, most programming languages include functions which generate 
  22. random numbers of some sort.  For instance, in BASIC, the function ^1RND^0 returns
  23. a random real number between zero and one.  To get a random integer from zero 
  24. to N-1, use ^1I = INT(RND * N)^0.
  25.  
  26. In Turbo Pascal, the equivalent construct is the function ^1random^0, which can
  27. be used in two different ways:  If it is called with no arguments, as in ^1r :=
  28. ^1random^0, it will generate a random real number between zero and one, just like
  29. the ^1RND^0 function of BASIC.  If, on the other hand, it is called with a single
  30. integer argument, as in ^1i := random(n)^0, it will generate a random integer in the
  31. range from zero to n-1.
  32.  
  33. These functions let you generate all the randomness you need.  However, it is 
  34. not enough to use just these functions, if you want truly random numbers.  
  35. After all, a computer is a very deterministic machine, following explicit
  36. instructions to the letter.  The "random" numbers it generates are actually
  37. the result of a mathematical formula contained in the PC's operating system.
  38. If left to itself, it will give you the exact same random numbers every time.
  39. That wouldn't be very good for a card game program; somebody who plays the 
  40. game frequently is likely to notice if he always gets the same hand.
  41.  
  42. But there is a solution; use the ^1RANDOMIZE^0 command in both BASIC and Pascal.
  43.  
  44. In BASIC, it can be invoked in the form ^1RANDOMIZE TIMER^0.  This causes the value
  45. of the system timer to be used to "seed" the random number generator.  This 
  46. should probably be invoked after the user has been prompted to press a key to 
  47. continue, to ensure that this instruction is not always invoked the exact same 
  48. number of milliseconds after a reboot, so that it gives different values.  It 
  49. is unlikely that any human will be able to time his actions to such a fine 
  50. degree as to press a key at an exact desired millisecond; so for all practical
  51. purposes, the random number generator will be seeded with an arbitrary, 
  52. unpredictable number.
  53.  
  54. Actually, ^1RANDOMIZE^0 can be invoked with any variable or value as an 
  55. argument.  This will cause the random number generator to be seeded with the 
  56. specified value.  If it is a fixed value, such as 23, then you will always get 
  57. the same sequence of random numbers every time you use that value as a seed.  
  58. This can be useful when testing your program, if you'd like to try it out
  59. repeatedly using the same "random" values.
  60.  
  61. Invoking ^1RANDOMIZE^0 with no parameters causes the seed value to be 
  62. requested from the user.  This is probably not a good idea in game programs, 
  63. where a user might be able to "cheat" by picking a seed value for which he 
  64. already knows the resulting random numbers, but it might be good for use in a 
  65. program you are testing, so that you can try several runs with the same seed 
  66. value, then try out a different one.
  67.  
  68. In Turbo Pascal, the ^1randomize^0 function, with no arguments, causes the random
  69. number generator to be arbitrarily seeded.  Apparently the system clock value 
  70. is used for this seed.
  71.  
  72. I've found that when programs using Turbo Pascal's ^1randomize^0 function are 
  73. invoked more than once within a few minutes, the random number generator tends 
  74. to get seeded with a similar value, since only very low-order bits in the 
  75. system clock have changed.  This causes the first couple of random numbers 
  76. generated after ^1randomize^0 to be similar.  However, the random number 
  77. generator soon diverges if there is even the most minuscule difference in the 
  78. seed value; thus, to ensure maximum randomness in your program, you might 
  79. generate a few "dummy" random numbers like this: 
  80.  
  81. ^C^1for i := 1 to 10 do r := random
  82.  
  83. at the beginning of your program, right after ^1randomize^0 is invoked.  After a
  84. few iterations, the random numbers will become reasonably random. 
  85.  
  86.  
  87. ^CTYPING IN LONG LISTINGS
  88.  
  89. Thanks to George Leritte for this hint:  If you have to type in a long program 
  90. from a listing on paper (such as those in old-fashioned paper magazines), then
  91. you probably know how hard it is to keep your eyes on the screen and the paper 
  92. at once; you probably keep losing your place and are likely to miss whole 
  93. lines.  There's a better way, though, if you have a tape recorder.  Simply 
  94. record your voice reading off the paper listing at a speed which is comfortable
  95. for you to type.  Then play back the tape, and type the program lines as you 
  96. hear them on the tape.
  97.  
  98. Better yet, how about subscribing to BIG BLUE DISK?  Unlike those paper 
  99. magazines, BIG BLUE DISK programs are ready to run.
  100.  
  101.                            --------------------------
  102.  
  103. That's all for now; if you have any hints, tips, suggestions, or requests for 
  104. help on a PC-related topic, please send them in to us; for details, see the 
  105. article, How To Disk Us.
  106.