home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / ScreenSavers / BackSpaceViews / MazeView.BackModule / RandomIntPicker.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  762 b   |  46 lines

  1. // Dave's RandomIntPicker.
  2. //
  3. // After several ints have been inserted by [obj insert:n], one unifomrly
  4. // randomly choice can be made by looking at [obj choice].  Any number of
  5. // calls to [obj choice] will return the same value, and should not
  6. // be followed by any more [obj insert:n] without first calling
  7. // [obj reset], which readies the object for a new bunch of choices.
  8.  
  9. #import <libc.h>
  10. #import "RandomIntPicker.h"
  11.  
  12. @implementation RandomIntPicker
  13.  
  14. - init
  15. {
  16.     [self reset];
  17.     return self;
  18. }
  19.  
  20. - reset
  21. {
  22.     count=0;
  23.     pickedInt=0;
  24.     return self;
  25. }
  26.  
  27. - (int) choice
  28. {
  29.     return pickedInt;
  30. }
  31.  
  32. - (int) count
  33. {
  34.     return count;
  35. }
  36.  
  37. - insert: (int)newInt;
  38. {
  39.     count++;
  40.     if (!(random()%count)) pickedInt=newInt;
  41.     return self;
  42. }
  43.  
  44. @end
  45.         
  46.