home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / apple2 / 24424 < prev    next >
Encoding:
Internet Message Format  |  1992-11-24  |  2.3 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!ames!data.nas.nasa.gov!taligent!apple!dlyons
  2. From: dlyons@Apple.COM (David A Lyons)
  3. Newsgroups: comp.sys.apple2
  4. Subject: Re: Random Number Generator Needed!
  5. Message-ID: <74636@apple.apple.COM>
  6. Date: 24 Nov 92 04:48:32 GMT
  7. References: <1992Nov19.154845.2806@brandonu.ca>
  8. Organization: Apple Computer Inc., Cupertino, CA
  9. Lines: 45
  10.  
  11. In article <1992Nov19.154845.2806@brandonu.ca> marshall@brandonu.ca (Edward S. Marshall) writes:
  12. >Yet another plea from a desperate psuedo-BASIC programmer:
  13. >
  14. >I'm in need of a SHORT and SIMPLE random-number generator for ML.  Something
  15. >that'll return a number between $00 and $FF at a variable memory location.
  16. >I've tried using a few locations for a seed, but nothing seems to come up
  17. >random enough.  Basically, I'm stuck.  Anyone who's got some code that can
  18. >help me out, PLEASE write!  If it helps, I'm running on an e//e.  Thanks in
  19. >advance!
  20.  
  21. Counting the time until the VBL signal inverts (bit 7 of $C019) is one
  22. possible way.  Works on the GS and IIe at least...not sure about the IIc
  23. or IIc Plus offhand.
  24.  
  25. The system randomizes $4E and $4F while waiting for the user to hit a key.
  26. You might be able to use that.
  27.  
  28. If you're looking for a pseudo-random algorithm, here's my favorite.  It
  29. cycles through all 255 nonzero values before repeating (just be sure not
  30. to seed it with zero).
  31.  
  32.       lda num
  33.       asl a
  34.       bcc done
  35.       eor #$87
  36. done  sta num
  37.  
  38. Simple, no?  Shift it left a bit; if you shifted out a 1 bit, exclusive or
  39. with $87, and the result is also the seed for next time.  To jumble the
  40. order better (so you don't get 1, 2, 4, 8, 16, 32, 64, 128 in order, for
  41. example, you can call the whole thing 11 times each time you want a number,
  42. and you still get all 255 values before it repeats).
  43.  
  44. For 16 bits, the mask is $1D87, and you can repeat 19 times for more jumbled
  45. results (there are longer masks that I don't remember offhand, and in general
  46. the jumble-count is 3 more than the number of bits in your values).
  47.  
  48. Source:  An article in Popular Electronics, probably in the early 80s.  I
  49. should dig that up sometime.
  50. -- 
  51. David A. Lyons, Apple Computer, Inc.      |   DAL Systems
  52. Apple II System Software Engineer         |   P.O. Box 875
  53. Internet:dlyons@apple.com                 |   Cupertino, CA 95015-0875
  54.  
  55. My opinions are my own, not Apple's.
  56.