home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / eiffel / 1408 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  6.4 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!doc.ic.ac.uk!uknet!mucs!cs.man.ac.uk!mod
  2. From: mod@cs.man.ac.uk (Mike O'Docherty (Teaching Company Associate))
  3. Newsgroups: comp.lang.eiffel
  4. Subject: Re: Need a random number generator
  5. Message-ID: <mod.726414289@cs.man.ac.uk>
  6. Date: 7 Jan 93 13:44:49 GMT
  7. References: <1993Jan7.082202.7901@city.cs>
  8. Sender: news@cs.man.ac.uk
  9. Lines: 187
  10.  
  11. mb108@cs.city.ac.uk (omo ADELAKUN T K) writes:
  12.  
  13. >Hi folks,
  14. >I need a (pseudo-)random number generator for use in a tester program
  15. >for a queuing system. The Eiffel libraries don't appear to have such
  16.  
  17. If you're talking about ISE Eiffel 2.3 on SunOS then try the following class.
  18. The man page for lrand48 explains the internals.
  19.  
  20. Mike.
  21.  
  22. -----------------8<------------------------------------------
  23.  
  24. -- A class supplying various random entities.
  25.  
  26. class RANDOMISER
  27.  
  28. export
  29.     seed, integral, alpha, alpha_string, word
  30.  
  31. feature
  32.  
  33.     words_length:INTEGER is 25000;
  34.     word_count:INTEGER;
  35.     
  36.     words:ARRAY[STRING];
  37.             -- where to put the words from /usr/dict/words
  38.  
  39.     Create(a_word_count:INTEGER) is
  40.             -- 'a_word_count' states how many words you want 'word' to
  41.         -- choose from
  42.     do
  43.             word_count := a_word_count;
  44.         end;
  45.     
  46.     seed(val:INTEGER) is
  47.                     -- Seed the random sequence. See srand48(1) for an
  48.             -- explanation of seeds.
  49.                 external
  50.                 srand48(seedval:INTEGER)
  51.                                 language "C";
  52.         once
  53.             srand48(val);
  54.         end; -- 'seed'
  55.  
  56.     word:STRING is
  57.             -- Provide a word picked at random from
  58.             -- /usr/dict/words.
  59.         local
  60.             word_file:FILE;
  61.             stp, i, j:INTEGER;
  62.         do
  63.             -- only get the words once per object
  64.             if words.Void then
  65.                 word_file.Create("/usr/dict/words");
  66.                 word_file.open_read;
  67.                 stp := words_length div word_count;
  68.                 words.Create(1, word_count);
  69.  
  70.                 -- choose a random start point among the first
  71.                 -- `stp' words and then read every `stp'th
  72.                 -- word
  73.                 from
  74.                     i := integral(1, stp)
  75.                 until
  76.                     i = stp
  77.                 loop
  78.                     word_file.next_line;
  79.                     i := i + 1;
  80.                 end;
  81.  
  82.                 from
  83.                     i := 1;
  84.                 until
  85.                     i > word_count
  86.                 loop
  87.                     word_file.readline;
  88.                     words.put(word_file.laststring.duplicate, i);
  89.                     if i < word_count then
  90.                         from
  91.                             j := 1
  92.                         until
  93.                             j = stp
  94.                         loop
  95.                             word_file.next_line;
  96.                             j := j + 1;
  97.                         end;
  98.                     end;
  99.                     i := i + 1;
  100.                 end;
  101.             end;
  102.  
  103.             -- return a random word from the generated collection
  104.             Result := words.item(integral(1, word_count)).duplicate;
  105.         ensure
  106.             good_word: not Result.Void and then Result.count /= 0;
  107.         end; -- 'word'
  108.  
  109.     integral(lower, upper:INTEGER):INTEGER is
  110.                     -- Return a random integer in the range
  111.             -- [lower .. upper].
  112.         external
  113.                         lrand48:INTEGER
  114.                                 language "C"
  115.         do
  116.             Result := (lrand48 mod (upper - lower + 1)) + lower;
  117.         end; -- 'integral'
  118.  
  119.     alpha_string(min_length, max_length:INTEGER):STRING is
  120.             -- Return a string of random letters (lower or upper
  121.             -- case) with length in the range
  122.             -- [min_length .. max_length].
  123.         local
  124.             i, stop_at:INTEGER;
  125.         do
  126.             from
  127.                 Result.Create(max_length);
  128.                 stop_at := integral(min_length, max_length);
  129.                 i := 1;
  130.             until
  131.                 i = stop_at + 1
  132.             loop
  133.                 Result.extend(alpha);
  134.                 i := i + 1;
  135.             end;
  136.         end; -- 'alpha_string'
  137.  
  138.     alpha:CHARACTER is
  139.             -- Return a random upper or lower case letter.
  140.         do
  141.             inspect integral(1, 52)
  142.             when 1 then Result := 'a';
  143.             when 2 then Result := 'b';
  144.             when 3 then Result := 'c';
  145.             when 4 then Result := 'd';
  146.             when 5 then Result := 'e';
  147.             when 6 then Result := 'f';
  148.             when 7 then Result := 'g';
  149.             when 8 then Result := 'h';
  150.             when 9 then Result := 'i';
  151.             when 10 then Result := 'j';
  152.             when 11 then Result := 'k';
  153.             when 12 then Result := 'l';
  154.             when 13 then Result := 'm';
  155.             when 14 then Result := 'n';
  156.             when 15 then Result := 'o';
  157.             when 16 then Result := 'p';
  158.             when 17 then Result := 'q';
  159.             when 18 then Result := 'r';
  160.             when 19 then Result := 's';
  161.             when 20 then Result := 't';
  162.             when 21 then Result := 'u';
  163.             when 22 then Result := 'v';
  164.             when 23 then Result := 'w';
  165.             when 24 then Result := 'x';
  166.             when 25 then Result := 'y';
  167.             when 26 then Result := 'z';
  168.             when 27 then Result := 'A';
  169.             when 28 then Result := 'B';
  170.             when 29 then Result := 'C';
  171.             when 30 then Result := 'D';
  172.             when 31 then Result := 'E';
  173.             when 32 then Result := 'F';
  174.             when 33 then Result := 'G';
  175.             when 34 then Result := 'H';
  176.             when 35 then Result := 'I';
  177.             when 36 then Result := 'J';
  178.             when 37 then Result := 'K';
  179.             when 38 then Result := 'L';
  180.             when 39 then Result := 'M';
  181.             when 40 then Result := 'N';
  182.             when 41 then Result := 'O';
  183.             when 42 then Result := 'P';
  184.             when 43 then Result := 'Q';
  185.             when 44 then Result := 'R';
  186.             when 45 then Result := 'S';
  187.             when 46 then Result := 'T';
  188.             when 47 then Result := 'U';
  189.             when 48 then Result := 'V';
  190.             when 49 then Result := 'W';
  191.             when 50 then Result := 'X';
  192.             when 51 then Result := 'Y';
  193.             when 52 then Result := 'Z';
  194.             end;
  195.         end; -- 'alpha'
  196.  
  197. end -- !class! /RANDOMISER/
  198.