यह ठीक नहीं हो सकता है तो आप इस कैसे लागू है, लेकिन उम्मीद है कि यह आप शुरू हो जाएगा।
#import <stdlib.h>
#import <time.h>
आपके क्रियान्वयन में कहीं:
//
// get count of entities
//
NSFetchRequest *myRequest = [[NSFetchRequest alloc] init];
[myRequest setEntity: [NSEntityDescription entityForName:myEntityName inManagedObjectContext:myManagedObjectContext]];
NSError *error = nil;
NSUInteger myEntityCount = [myManagedObjectContext countForFetchRequest:myRequest error:&error];
[myRequest release];
//
// add another fetch request that fetches all entities for myEntityName -- you fill in the details
// if you don't trigger faults or access properties this should not be too expensive
//
NSArray *myEntities = [...];
//
// sample with replacement, i.e. you may get duplicates
//
srandom(time(NULL)); // seed random number generator, so that you get a reasonably different series of random integers on each execution
NSUInteger numberOfRandomSamples = ...;
NSMutableSet *sampledEntities = [NSMutableSet setWithCapacity:numberOfRandomSamples];
for (NSInteger sampleIndex = 0; sampleIndex < numberOfRandomSamples; sampleIndex++) {
int randomEntityIndex = random() % myEntityCount; // generates random integer between 0 and myEntityCount-1
[sampledEntities addObject:[myEntities objectAtIndex:randomEntityIndex]];
}
// do stuff with sampledEntities set
आप प्रतिस्थापन के बिना नमूने के लिए, डुप्लिकेट को खत्म करने की जरूरत है, तो आप शायद अपने शीर्षक में या अपने कार्यान्वयन फ़ाइल के शीर्ष पर
कहीं randomEntityIndex
NSNumber
ऑब्जेक्ट्स को केवल यादृच्छिक int
एस नमूना देने के बजाय बनाएं।
इस मामले में, एक आदेश दिया NSSet
से नमूना, के रूप में आप उन्हें बैग से बाहर निकालते हैं और सेट से एक यादृच्छिक NSNumber
वस्तु उठा के प्रयोजनों के लिए myEntityCount
घटती NSNumber
वस्तुओं को हटा दें।
स्रोत
2010-05-13 22:17:08
अतिरिक्त जानकारी: जिन चीज़ों को मैं पकड़ने की कोशिश कर रहा हूं वे दो गुणों के साथ ऑब्जेक्ट्स हैं, 1-50 वर्णों से कहीं भी एक एनएसस्ट्रिंग, और एक छद्म प्राथमिक कुंजी int मैंने सोचा था कि यादृच्छिक चयन बिट में मदद मिल सकती है। मैं मॉडल को पुन: व्यवस्थित कर सकता हूं, हालांकि यदि आवश्यक हो तो भी इस चूसने वाले को प्रोटोटाइप करना। – nickthedude
यह टिप्पणी आपके प्रश्न में जोड़ा जाना चाहिए। –