This is RandomIntPicker.m in view mode; [Download] [Up]
// Dave's RandomIntPicker.
//
// After several ints have been inserted by [obj insert:n], one unifomrly
// randomly choice can be made by looking at [obj choice]. Any number of
// calls to [obj choice] will return the same value, and should not
// be followed by any more [obj insert:n] without first calling
// [obj reset], which readies the object for a new bunch of choices.
#import <libc.h>
#import "RandomIntPicker.h"
@implementation RandomIntPicker
- init
{
[self reset];
return self;
}
- reset
{
count=0;
pickedInt=0;
return self;
}
- (int) choice
{
return pickedInt;
}
- (int) count
{
return count;
}
- insert: (int)newInt;
{
count++;
if (!(random()%count)) pickedInt=newInt;
return self;
}
@end
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.