home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 5971 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: ix.netcom.com!news
  2. From: jkarcher@ix.netcom.com(John J. Karcher )
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Random #s
  5. Date: 22 Mar 1996 17:17:17 GMT
  6. Organization: Netcom
  7. Message-ID: <4iunat$jnj@dfw-ixnews3.ix.netcom.com>
  8. References: <4ihkif$ed0@apollo.isisnet.com> <4irnj8$a2t@dunlop.cs.strath.ac.uk>
  9. NNTP-Posting-Host: ix-vf4-17.ix.netcom.com
  10. X-NETCOM-Date: Fri Mar 22 11:17:17 AM CST 1996
  11.  
  12. In <4irnj8$a2t@dunlop.cs.strath.ac.uk> pcallagh@cs.strath.ac.uk (Paul
  13. Callaghan) writes: 
  14. >
  15. >Here is a little program which will generate random numbers
  16. >(Off the top of my head so it `should` work.)
  17. >(rand() % MUTATE)+
  18. >#include <time.h>
  19. >#define HI 6
  20. >#define LO 1
  21. >
  22. >
  23. >void main()
  24. >{
  25. > int rand_no;
  26. >
  27. > srand(time(NULL));
  28. >
  29. > rand_no=(rand() % HI)+LO;
  30. >}
  31. >
  32. >This generates a random number between HI and LO every time the
  33. >program is run.
  34. >(Doesn`t do anything with it bet generates it anyway :-)<-<)    
  35.  
  36. If my eyes don't deceive me (which they sometimes do), this code will 
  37. work only if LO equals 1.  Shouldn't this read:
  38.  
  39.  rand_no = (rand() % (HI - LO)) + LO;
  40.  
  41. to get a number where LO <= rand_no < HI, or
  42.  
  43.  rand_no = (rand() % ((HI - LO) + 1)) + LO;
  44.  
  45. to get a number where LO <= rand_no <= HI.
  46.  
  47. Not to be picky... :^)
  48.  
  49.      John J. Karcher
  50.  
  51.