home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 3811 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  102 lines

  1. Path: news.iastate.edu!pholland
  2. From: pholland@iastate.edu (Paul J Hollander)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Random Number Generation
  5. Date: 24 Feb 1996 18:51:02 GMT
  6. Organization: Iowa State University, Ames, Iowa USA
  7. Message-ID: <4gnmmm$ru9@news.iastate.edu>
  8. References: <199602071623.QAA00075@sable.ox.ac.uk> <1100.6613T1273T666@himolde.no> <692.6619T1254T1752@in.net> <4g2bi9$1fls@rs18.hrz.th-darmstadt.de>
  9. NNTP-Posting-Host: du139-206.cc.iastate.edu
  10.  
  11. >In article <692.6619T1254T1752@in.net>, mave@in.net (John J. Maver, Jr.) writes:
  12. >> 
  13. >>         I want to generate a randum number, like BASIC allowed you to do, in
  14. >> C.  I want a function that I can call with the High and Low numbers and then
  15. >> have it return a number between them.
  16. >> 
  17. >> 
  18. >> What I have so far is:
  19. >> 
  20. >> <sb>
  21. >> 
  22. >> float rndnum(float low, float high)
  23. >> {
  24. >>     float num;     /*used to temporarily hold a rnd num*/
  25. >> 
  26. >> 
  27. >>         srand=(time(NULL));     /*generate a new seed*/
  28. >>         num=rand()%1000;        /*Try to modify rand()s output to be*/
  29. >>         num/=100;               /*between 0 and 1*/
  30. >>         num=(num*high)+low;     /*Make num fit the specified range*/
  31. >> 
  32. >>         return num;
  33. >> }
  34. >> 
  35. >>         It doesn't quite work.  Any hints?
  36. >> 
  37. >> <sb>
  38.  
  39. I needed a random number generator a few weeks ago and hacked this out
  40. figuring out how to do it.  System: A3000/25, 6 MB, OS 3.1, SAS/C 5.10.
  41. Hope it helps.
  42.  
  43. I wonder if the problem is in the line num=(num*high)+low.  If so, try
  44. changing it to num = (num * (high - low + 1)) + low.
  45.  
  46. Paul Hollander    pholland@iastate.edu
  47. Behold the tortoise: he makes no progress unless he sticks his neck out.
  48.  
  49. -------------------
  50.  
  51. /* Title = Random_Num_Gen */
  52. /*  Random number generator using the time clock
  53.     Generates integer random numbers from 10 to 20, 
  54.       including both 10 and 20
  55.  
  56.     Drand48() and srand48() require math.h
  57.     Time() and ctime() require time.h
  58. */
  59.  
  60. #include <stdio.h>
  61. #include <math.h>
  62. #include <time.h>
  63.  
  64. void main()
  65. {
  66.     float rnd;
  67.     int L1, bottom, top, interval, x;
  68.     long t;
  69.  
  70.     bottom = 10;
  71.     top = 20;
  72.     interval = top - bottom + 1;
  73.     L1 = x = 0;
  74.  
  75. /* Put the number of seconds in the time clock into variable t */
  76.  
  77.     time (&t);
  78.     printf ("Seconds = %d\n", t);
  79.     printf ("Current time is %s\n", ctime(&t));
  80.  
  81. /* Seed the randomize function with t */
  82.  
  83.     srand48(t);
  84.  
  85. /* The drand48() function gets a random number >= 0.0 and < 1.0 */
  86.  
  87.     for (L1 = 1; L1 <= 10; L1++)
  88.     {
  89.         rnd = drand48();
  90.         if (L1 == 9)
  91.             rnd = .0;
  92.         if (L1 == 10)
  93.             rnd = .999999;
  94.         x = rnd * interval + bottom;
  95.         printf ("Random number = %6f\n", rnd);
  96.         printf ("  %d <= %d <= %d\n", bottom, x, top);
  97.     }
  98.  
  99.     printf ("\n\n");
  100. }
  101.  
  102.