home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / eiffel / 1406 < prev    next >
Encoding:
Text File  |  1993-01-07  |  2.9 KB  |  114 lines

  1. Newsgroups: comp.lang.eiffel
  2. Path: sparky!uunet!gatech!paladin.american.edu!howland.reston.ans.net!zaphod.mps.ohio-state.edu!malgudi.oar.net!caen!uvaarpa!murdoch!cds001.cebaf.gov!quarrie
  3. From: quarrie@cds001.cebaf.gov (David Quarrie)
  4. Subject: Re: Need a random number generator
  5. Message-ID: <1993Jan7.134255.14773@murdoch.acc.Virginia.EDU>
  6. Sender: usenet@murdoch.acc.Virginia.EDU
  7. Reply-To: quarrie@cds001.cebaf.gov (David Quarrie)
  8. Organization: CEBAF (Continuous Electron Beam Accelerator Facility)
  9. References:  <1993Jan7.082202.7901@city.cs>
  10. Date: Thu, 7 Jan 1993 13:42:55 GMT
  11. Lines: 101
  12.  
  13. --
  14. In article <1993Jan7.082202.7901@city.cs>, mb108@cs.city.ac.uk (omo
  15. ADELAKUN T K) writes:
  16. |>Hi folks,
  17. |>I need a (pseudo-)random number generator for use in a tester program
  18. |>for a queuing system. The Eiffel libraries don't appear to have such
  19. |>a class - that is, according to grep. I can't see anything that looks
  20. |>like a library of binaries against I can run nm. (I'm working
  21. |>remotely
  22. |>from the labs and have no access to manuals.) 
  23. |>
  24. |>If there _is_ one I'd like to know where to find it, and if there
  25. |>isn't
  26. |>one I'd appreciate pointers to public-domain sources (FTP?) of such
  27. |>classes. (I'm being a pedant for software reuse... 8-)
  28. |>
  29. |>
  30. |>Thanx++,            -- Oh, dear..! That syntax!
  31. |>Toyin.
  32. |>
  33.  
  34. I assume you want something that's uniform in the range 0.0-1.0. What I did
  35. was the following:
  36.  
  37. Create a class (I called it MORE_MATH since it contained some functions
  38. missing from the MATH) class (Eiffel 2.3.4 syntax):
  39.  
  40. class MORE_MATH export
  41.  
  42.     arctangent, exp, random
  43.  
  44. feature
  45.  
  46.     exp (x: REAL): REAL is
  47.             -- Exponential function of x.
  48.         external
  49.         c_exp:REAL language "C"
  50.         do            
  51.         Result := c_exp (x)
  52.         end; -- exp
  53.  
  54.     arctangent (x: REAL): REAL is
  55.             -- Arctangent of x.
  56.         external
  57.         c_atan:REAL language "C"
  58.         do            
  59.         Result := c_atan (x)
  60.         end; -- arctangent
  61.  
  62.     random: REAL is
  63.             -- Random Number generator in range 0.0-1.0.
  64.         external
  65.         c_rndm: REAL language "C"
  66.         do            
  67.         Result := c_rndm
  68.         end; -- random
  69.  
  70. end -- class MORE_MATH
  71.  
  72. Alongside of this I created a more_math.c file containing:
  73.  
  74. #include <math.h>
  75. #include <values.h>
  76.  
  77. float c_atan();
  78. float c_exp(), c_rndm();
  79.  
  80. float c_atan(x)
  81. float x;
  82. {
  83.     return ((float) atan ((double) x));
  84. }
  85.  
  86. float c_rndm()
  87. {
  88.     float f1, f2;
  89.  
  90.     f1 = random();
  91.     f2 = MAXINT;
  92.     return (f1/f2);
  93. }
  94.  
  95. float c_exp(x)
  96. float x;
  97. {
  98.     return (exp(x));
  99. }
  100.  
  101. Compile the more_math.c file and create an archive file from it and add that
  102. to the EXTERNALS section of your SDF (.eiffel) file. I'm not sure whether
  103. the C random() function is generic Unix, but I'm fairly sure that rand() is
  104. so you could use that instead.
  105.  
  106.         David
  107.  
  108. -------------------------------------------------------------------------------
  109.     David Quarrie                    CEBAF MS 12H
  110.                             12000 Jefferson Ave
  111. Internet: quarrie@cebaf.gov                Newport News VA 23606
  112. DECnet  : cebaf::quarrie                Tel: (804) 249-7562
  113. BITNET  : quarrie@cebaf                    Fax: (804) 249-5800
  114.