Shuffled array uncaught exception - iphone

Im using four buttons and shuffle the button tag value to view different values on each click, Since i show the value of button from array, While am trying to shuffle the tag value i got following error.
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray exchangeObjectAtIndex:withObjectAtIndex:]: index 16 beyond bounds [0 .. 3]'
My code for shuffling the array of tag value,
words = [[NSMutableArray alloc] initWithObjects:#"1", #"2", #"3",#"4", nil] ;
NSUInteger count = [questar count];
for (NSUInteger i = 0; i < count; ++i) {
// Select a random element between i and end of array to swap with.
NSInteger nElements = count - i;
NSInteger n = (arc4random() % nElements) + i;
[words exchangeObjectAtIndex:i withObjectAtIndex:n];
}
What change should i made??Can any one help me to solve

You have to select an image from quester first. You created the index n to take object from quester array no?
words = [[NSMutableArray alloc] initWithObjects:#"1", #"2", #"3",#"4", nil] ;
NSUInteger count = [questar count];
for (NSUInteger i = 0; i < count; ++i)
{
// Select a random element between i and end of array to swap with.
NSInteger nElements = count - i;
NSInteger n = (arc4random() % nElements) + i;
currentImage = [questar objectAtIndex:n];
[words replaceObjectAtIndex:i withObject:currentImage];
}
Or if you want to exchange within the array change count as :
NSUInteger count = [words count];

[words exchangeObjectAtIndex:i withObjectAtIndex:n];
In the above statement, both i and n should have a value less than the words count (here it is 4). Else, you would get exception.

The following:
NSInteger nElements = count - i;
NSInteger n = (arc4random() % nElements) + i;
Should be changed to:
NSInteger nElements = count;
NSInteger n = (arc4random() % nElements);

Related

shuffle array repeats One value

Here i used the following code to shuffle the NSMuttable Array, While shuffling The array repeats only one value and remaining all works properly, what change should i made to shuffle without repeating?
words = [[NSMutableArray alloc] initWithObjects:#"1", #"2", #"3",#"4", nil] ;
NSUInteger count = [words count];
for (int i = 0; i < count; i++)
{
NSInteger nElements = count - i;
NSInteger n = (arc4random() % nElements) + i;
NSLog(#"n val %d i val %d",i,n);
[words exchangeObjectAtIndex:i withObjectAtIndex:n];
}
NSlog Output
2012-12-31 12:49:05.730 quizer[1607:c07] n val 0 i val 0
2012-12-31 12:49:05.731 quizer[1607:c07] n val 1 i val 3
2012-12-31 12:49:05.731 quizer[1607:c07] n val 2 i val 2
2012-12-31 12:49:05.732 quizer[1607:c07] n val 3 i val 3
Please Help me to solve
This code may help you,
The shuffle array contains each number between 0 to TOTAL_NUMBER (your array size) only once.
From which you can shuffle your existing array.
#synthesize alreadyGeneratedNumbers;//this is a mutableArray.
int TOTAL_NUMBER=5;//size of array
-(int)generateRandomNumber{
int low_bound = 0;
int high_bound = TOTAL_NUMBER;
int width = high_bound - low_bound;
int randomNumber = low_bound + arc4random() % width;
return randomNumber;
}
-(IBAction)randomNumbers:(UIButton *)sender
{
NSMutableArray *startArray=[NSMutableArray arrayWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
NSMutableArray *shuffle = [NSMutableArray new];
BOOL contains=YES;
while ([shuffle count]<5) {
NSNumber *generatedNumber=[NSNumber numberWithInt:[self generateRandomNumber]];
//NSLog(#"->%#",generatedNumber);
if (![alreadyGeneratedNumbers containsObject:generatedNumber]) {
[shuffle addObject:generatedNumber];
contains=NO;
[alreadyGeneratedNumbers addObject:generatedNumber];
}
}
if ([alreadyGeneratedNumbers count] >= TOTAL_NUMBER) {
NSLog(#"\nOne shuffel ended.....what to reshuffle");
[alreadyGeneratedNumbers removeAllObjects];
}
NSMutableArray *shuffledArray=[NSMutableArray new];
for (NSNumber *index in shuffle) {
[shuffledArray addObject:[startArray objectAtIndex:[index integerValue]]];
}
NSLog(#"Shuffled array = %#",shuffledArray);
}
To remove duplicates from your NSMutableArray,
// Removing duplicate entry from array
NSArray *copy = [words copy];
NSInteger index = [copy count] - 1;
for (id object in [copy reverseObjectEnumerator])
{
if ([words indexOfObject:object inRange:NSMakeRange(0, index)] != NSNotFound)
{
[words removeObjectAtIndex:index];
}
index--;
}
[copy release];
You can check it for each time you add the object.

Generating a unique number [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to generate a unique number for a bingo app, now its choses at 90 random numbers between 1-90 and adds them to a NSMutableSet. That all works, but I get the number picked from the set to be unique, so the same number is pulled out twice.
Here is what I have so far:
NSMutableSet * numberSet1 = [NSMutableSet setWithCapacity:90];
while ([numberSet1 count] < 90 ) {
NSNumber * randomNumber1 = [NSNumber numberWithInt:(arc4random() % 90 + 1)];
[numberSet1 addObject:randomNumber1];
}
//NSLog(#"numberWithSet : %# \n\n",numberSet1);
NSArray * numbers = [numberSet1 allObjects];
//to display
int r = arc4random() % [numbers count];
if(r<[numbers count]){
numberLabel.text = [NSString stringWithFormat:#"%#", [numbers objectAtIndex:r]];
}
How can I stop it from giving me duplicates?
Thanks in advance
As an alternative to picking random numbers with arc4random() (which samples with replacement, which you probably don't want for a Bingo game):
Take an array of numbers 1 through 90.
Shuffle that array.
Pick the first number from the array, then the second, and so on.
Keep an index of the currently selected element in the array. To get the next number, increment and check if you are dereferencing the 90th element.
Some pseudo-code:
#define SIZE 90
unsigned int index;
unsigned int elements[SIZE];
/* step 1 -- populate the array with elements 1 through 90 */
for (index = 0; index < SIZE; index++)
elements[index] = index + 1;
/* step 2 -- shuffle the array */
fisher_yates_shuffle(elements);
/* step 3 -- read from the array */
for (index = 0; index < SIZE; index++)
fprintf(stdout, "element at index %u is %u\n", index, elements[index]);
This creates and shuffles an array.
// array to shuffle
NSMutableArray *array = [NSMutableArray array];
for(int i=1; i<91; i++){
[array addObject:[NSNumber numberWithInt:i]];
}
// shuffle
for (int i = [array count]-1; i>0; i--) {
[array exchangeObjectAtIndex:i withObjectAtIndex:arc4random()%(i+1)];
}
Not sure if this is what you want since picking 90 numbers at random from 1-90 with no duplicates and adding them to a mutable set is no different from adding everything to a mutable set. But if you want a subset of the 90 numbers just take up to n elements from the array.
From a lottery app I wrote a while back, also applicable here-
int ball[6], counter, check, similarity;
for ( counter = 1; counter <= 6; counter++ )
{
for (;;)
{
similarity = 0;
ball[counter] = 1 + arc4random() % 6;
for (check = counter-1; check >= 1;check--)
{
if ( ball[check] == ball[counter] )
{
similarity = 1;
break;
}
}
if(similarity == 0)
break;
}
printf("%i\n", ball[counter]);
}
Checks the chosen ball with all previous balls, will never get the same number twice.
Create a category on NSMutableArray
#implementation NSMutableArray (RandomUtils)
-(void)shuffle
{
NSUInteger count = [self count];
for (NSUInteger i = 0; i < count; ++i) {
NSUInteger nElements = count - i;
NSUInteger n = (arc4random() % nElements) + i;
[self exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
#end
You can also wrap it for a NSArray version
#implementation NSArray (RandomUtils)
-(NSMutableArray *)mutableArrayShuffled
{
NSMutableArray *array = [[self mutableCopy] autorelease];
[array shuffle];
return array;
}
-(NSArray *)arrayShuffled
{
return [NSArray arrayWithArray:[self mutableArrayShuffled]];
}
#end
Where needed, import the category, and do:
NSMutableArray *array = [NSMutableArray array];
for(int i=0; i<90; i++){
[array addObject:[NSNumber numberWithInt:i+1]];
}
[array shuffle];
NSArray *newarray = [array subarrayWithRange:NSMakeRange(0,6)];
Another approach could be to fill a set with 6 random picks.
in the Category:
#implementation NSArray (RandomUtils)
-(id)randomElement
{
if ([self count] < 1) return nil;
NSUInteger randomIndex = arc4random() % [self count];
return [self objectAtIndex:randomIndex];
}
#end
use it like:
NSMutableSet *mset = [NSMutableSet set];
while ([mset count]<6) {
[mset addObject:[array randomElement]];
}

Error when trying to shuffle an NSMutableArray

I'm trying to shuffle an NSMutableArray so that its order will be mixed up every-time someone loads the view.
In my -(void)viewDidLoad I'm putting the following code (as suggested by other users):
NSMutableArray *shuffleTwo = [self.chosenTeamDict objectForKey:#"clubs"];
int random = arc4random() % [shuffleTwo count];
for (int i = 0; i < [shuffleTwo count]; i++) {
[shuffleTwo exchangeObjectAtIndex:random withObjectAtIndex:i];
}
NSLog(#"%#", shuffleTwo);
But when I do this and try and run the page, I get the following error:
2012-07-09 18:42:16.126 Kit-Quiz[6505:907] (null)
libc++abi.dylib: terminate called throwing an exception
Can anyone advice either a new way of shuffling this array, or advice me on how to avoid this error..!? I'm building for iOS 5 and I'm using Xcode45-DP1. Thanks in advance!
(EDIT)
I've also tried this method and I get the same error:
NSMutableArray *shuffledArray = [[NSMutableArray alloc] init];
NSMutableArray *standardArray = [self.chosenTeamDict objectForKey:#"clubs"];
for(int s = 0; s < [standardArray count]; s++){
int random = arc4random() % s;
[shuffledArray addObject:[standardArray objectAtIndex:random]];
}
NSLog(#"%#", shuffledArray);
NSMutableArray *standardArray = [self.chosenTeamDict objectForKey:#"clubs"];
int length = 10; // int length = [yourArray count];
NSMutableArray *indexes = [[NSMutableArray alloc] initWithCapacity:length];
for (int i=0; i<10; i++) [indexes addObject:[shuffledArray objectAtIndex:i]];
NSMutableArray *shuffle = [[NSMutableArray alloc] initWithCapacity:length];
while ([indexes count])
{
int index = rand()%[indexes count];
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
}
for (int i=0; i<[shuffle count]; i++) NSLog(#"%#", [shuffle objectAtIndex:i]);
NSLog(#"%#", shuffle);
^^ ANSWER
Try Fisher-Yates shuffle. It goes like this:
int count = shuffledArray.count;
for(int i=count; i>0; i--) {
int j = arc4random_uniform(count);
[shuffledArray exchangeObjectAtIndex:j withObjectAtIndex:i];
}
make sure that your array is non-nil and all the entries are allocated objects :)
Source: Fisher-Yates Shuffle
First, you really should enable exception breakpoints. In XCode on the left-hand panel, click the breakpoint tab, click the "+" sign at the bottom-left -> exception breakpoint -> done.
I suspect your problem lies here:
int random = arc4random() % [shuffleTwo count];
If [shuffleTwo count] evaluates to zero (also if shuffleTwo is nil) it will throw a division by zero exception. Edit: Doesn't seem to be the case in Objective-C.

Permutations/Anagrams in Objective-C -- I am missing something

(code below regarding my question)
Per this stack overflow question I used Pegolon's approach to generating all possible permutations of a group of characters inside an NSString. However, I am now trying to get it to not just generate an ANAGRAM which is all permutations of the same length, but all possible combinations (any length) of the characters in a string.
Would anyone know how i would alter the following code to get it to do this? This is much like: Generate All Permutations of All Lengths -- but (for fear of them needing answer to homework) they did not leave code. I have a sample of what I thought would do it at the bottom of this post... but it did not.
So, the code, as is, generates the, teh, hte, het, eth and eht when given THE.
What I need is along the lines of: t,h,e,th,ht,te,he (etc) in addition to the above 3 character combinations.
How would I change this, please. (ps: There are two methods in this. I added allPermutationsArrayofStrings in order to get the results back as strings, like I want them, not just an array of characters in another array). I am assuming the magic would happen in pc_next_permutation anyway -- but thought I would mention it.
In NSArray+Permutation.h
#import <Foundation/Foundation.h>
#interface NSArray(Permutation)
- (NSArray *)allPermutationsArrayofArrays;
- (NSArray *)allPermutationsArrayofStrings;
#end
in NSArray+Permutation.m:
#define MAX_PERMUTATION_COUNT 20000
NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size);
NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size)
{
// slide down the array looking for where we're smaller than the next guy
NSInteger pos1;
for (pos1 = size - 1; perm[pos1] >= perm[pos1 + 1] && pos1 > -1; --pos1);
// if this doesn't occur, we've finished our permutations
// the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
if (pos1 == -1)
return NULL;
assert(pos1 >= 0 && pos1 <= size);
NSInteger pos2;
// slide down the array looking for a bigger number than what we found before
for (pos2 = size; perm[pos2] <= perm[pos1] && pos2 > 0; --pos2);
assert(pos2 >= 0 && pos2 <= size);
// swap them
NSInteger tmp = perm[pos1]; perm[pos1] = perm[pos2]; perm[pos2] = tmp;
// now reverse the elements in between by swapping the ends
for (++pos1, pos2 = size; pos1 < pos2; ++pos1, --pos2) {
assert(pos1 >= 0 && pos1 <= size);
assert(pos2 >= 0 && pos2 <= size);
tmp = perm[pos1]; perm[pos1] = perm[pos2]; perm[pos2] = tmp;
}
return perm;
}
#implementation NSArray(Permutation)
- (NSArray *)allPermutationsArrayofArrays
{
NSInteger size = [self count];
NSInteger *perm = malloc(size * sizeof(NSInteger));
for (NSInteger idx = 0; idx < size; ++idx)
perm[idx] = idx;
NSInteger permutationCount = 0;
--size;
NSMutableArray *perms = [NSMutableArray array];
do {
NSMutableArray *newPerm = [NSMutableArray array];
for (NSInteger i = 0; i <= size; ++i)
[newPerm addObject:[self objectAtIndex:perm[i]]];
[perms addObject:newPerm];
} while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
free(perm);
return perms;
}
- (NSArray *)allPermutationsArrayofStrings
{
NSInteger size = [self count];
NSInteger *perm = malloc(size * sizeof(NSInteger));
for (NSInteger idx = 0; idx < size; ++idx)
perm[idx] = idx;
NSInteger permutationCount = 0;
--size;
NSMutableArray *perms = [NSMutableArray array];
do {
NSMutableString *newPerm = [[[NSMutableString alloc]initWithString:#"" ]autorelease];
for (NSInteger i = 0; i <= size; ++i)
{
[newPerm appendString:[self objectAtIndex:perm[i]]];
}
[perms addObject:newPerm];
} while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
free(perm);
return perms;
}
#end
My code that I thought would fix this:
for ( NSInteger i = 1; i <= theCount; i++) {
NSRange theRange2;
theRange2.location = 0;
theRange2.length = i;
NSLog(#"Location: %i (len: %i) is: '%#'",theRange2.location,theRange2.length,[array subarrayWithRange:theRange2]);
NSArray *allWordsForThisLength = [[array subarrayWithRange:theRange2] allPermutationsArrayofStrings];
for (NSMutableString *theString in allWordsForThisLength)
{
NSLog(#"Adding %# as a possible word",theString);
[allWords addObject:theString];
}
I know it would not be the most efficient..but I was trying to test.
This is what I got:
2011-07-07 14:02:19.684 TA[63623:207] Total letters in word: 3
2011-07-07 14:02:19.685 TA[63623:207] Location: 0 (len: 1) is: '(
t
)'
2011-07-07 14:02:19.685 TA[63623:207] Adding t as a possible word
2011-07-07 14:02:19.686 TA[63623:207] Location: 0 (len: 2) is: '(
t,
h
)'
2011-07-07 14:02:19.686 TA[63623:207] Adding th as a possible word
2011-07-07 14:02:19.687 TA[63623:207] Adding ht as a possible word
2011-07-07 14:02:19.688 TA[63623:207] Location: 0 (len: 3) is: '(
t,
h,
e
)'
2011-07-07 14:02:19.688 TA[63623:207] Adding the as a possible word
2011-07-07 14:02:19.689 TA[63623:207] Adding teh as a possible word
2011-07-07 14:02:19.690 TA[63623:207] Adding hte as a possible word
2011-07-07 14:02:19.691 TA[63623:207] Adding het as a possible word
2011-07-07 14:02:19.691 TA[63623:207] Adding eth as a possible word
2011-07-07 14:02:19.692 TA[63623:207] Adding eht as a possible word
As you can see, no one or two letter words -- I am pulling my hair out! (and I don't have much to spare!)
An easy thing to do would be to take all subsets of size k and use the code you have to generate all permutations of the subset. This is easy, but not the most efficient.
Here's a better approach. You are generating permutations lexicographically in the first routine:
1234
1243
1324
1342
1423
...
Each time you call NSInteger *pc_next_permutation(NSInteger *perm, const NSInteger size), you get the next permutation in lex order by finding the correct position to change. When you do that, truncate from the spot you changed to get the following:
1234 123 12 1
1243 124
1324 132 13
1342 134
1423 142 14
1432 143
2143 214 21 2
...
I hope the idea is clear. Here's one way to implement this (in Objective C-like pseudocode).
-(NSMutableArray *)nextPerms:(Perm *)word {
int N = word.length;
for (int i=N-1; i > 0; ++i) {
if (word[i-1] < word[i]) {
break;
} else if (i==1) {
i = 0;
}
}
// At this point, i-1 is the leftmost position that will change
if (i == 0) {
return nil;
}
i = i-1;
// At this point, i is the leftmost position that will change
Perm *nextWord = word;
for (int j=1; j <= N-i; ++j) {
nextWord[i+j] = word[N-j];
}
nextWord[i] = nextWord[i+1];
nextWord[i+1] = word[i];
// At this point, nextPerm is the next permutation in lexicographic order.
NSMutableArray *permList = [[NSMutableArray alloc] init];
for (int j=i; j<N; ++j) {
[permList addObject:[nextWord subwordWithRange:NSMakeRange(0,i)]];
}
return [permList autorelease];
}
This will return an array with the partial permutations as described above. The input for nextPerms should be the lastObject of the output of nextPerms.
Okay,
Down and dirty for now, however, this is what I did...
I changed the NSArray+Permutations.m to be as follows:
- (NSArray *)allPermutationsArrayofStrings
{
NSInteger size = [self count];
NSInteger *perm = malloc(size * sizeof(NSInteger));
for (NSInteger idx = 0; idx < size; ++idx)
perm[idx] = idx;
NSInteger permutationCount = 0;
--size;
NSMutableArray *perms = [NSMutableArray array];
NSMutableDictionary *permsDict = [[NSMutableDictionary alloc] init];
do {
NSMutableString *newPerm = [[[NSMutableString alloc]initWithString:#"" ]autorelease];
for (NSInteger i = 0; i <= size; ++i)
{
[newPerm appendString:[self objectAtIndex:perm[i]]];
}
if ([permsDict objectForKey:newPerm] == nil)
{
[permsDict setObject:#"1" forKey:newPerm];
[perms addObject:newPerm];
}
for (NSInteger i = 1; i <= [newPerm length]; ++i)
{
NSRange theRange;
theRange.location = 0;
theRange.length = i;
if ([permsDict objectForKey:[newPerm substringToIndex:i]] == nil)
{
[permsDict setObject:#"1" forKey:[newPerm substringToIndex:i]];
[perms addObject:[newPerm substringToIndex:i]];
}
}
} while ((perm = pc_next_permutation(perm, size)) && ++permutationCount < MAX_PERMUTATION_COUNT);
free(perm);
[permsDict release];
return perms;
}
The major changes were the idea #PengOne had... Return the resulting lexically changed string but also shorten it by 1 character at a time and add that to the returned array if it did not exist already.
I chose to be "cheap" about it and keep track using a NSMutableDictionary. So if the lexically changed string was not listed in the dictionary, it was added.
Is that more-or-less what you thought I should do, #PengOne?
WAY faster than adding them all and dealing with the resulting duplicates later -- and I think it works like I need it to.

Assigning a button tag from an array which stores integers

I am trying to assign a tag to button. The normal command is:
button.tag = 1;
The tag must be an integer.
My problem is that I would like to assign an integer which I stored in an array (tabReference) which is yet again part of a class (currentNoteBook). So I need this:
int k = 0;
button.tag = [currentNoteBook.tabReference objectAtIndex:k]; // This is where I get the warning.
This doesn't seem to work, however, as xCode tells me: Passing argument 1 of setTag: makes integer from pointer without a cast.
My array looks like this (I tried to use integers...):
NSMutableArray *trArray = [[NSMutableArray alloc] init];
NSNumber *anumber = [NSNumber numberWithInteger:1];
[trArray addObject: anumber];
[trArray addObject: anumber];
[trArray addObject: anumber];
[trArray addObject: anumber];
currentNoteBook.tabReference = trArray;
An NSMutableArray stores a modifiable array of objects. You can't directly store an integer in an NSMutableArray. That's why you have to do something like this to store a bunch of integers:
NSMutableArray *the_array = [[NSMutableArray alloc] init];
int max = 100;
for (int i = 0; i < max; i++)
{
NSNumber *temp_number = [NSNumber numberWithInt:arc4random() % max];
[the_array addObject:temp_number];
}
Of course, you could do pretty much the same thing and store something else in there:
NSMutableArray *the_array = [[NSMutableArray alloc] init];
int max = 100;
int max_x = 50;
int max_y = 25;
int max_w = 100;
int max_h = 200;
for (int i = 0; i < max; i++)
{
CGFloat temp_x = arc4random() % max_x;
CGFloat temp_y = arc4random() % max_y;
CGFloat temp_w = arc4random() % max_w;
CGFloat temp_h = arc4random() % max_h;
CGRect temp_rect = CGRectMake(temp_x, temp_y, temp_w, temp_h);
[the_array addObject:[NSValue valueWithCGRect:temp_rect]];
}
When you go to retrieve these values you need to specify what it is you want out of the array because the same array can contain very different objects.
For your integers:
for (int i = 0; i < max; i++)
{
NSLog(#"%i: %i", i, [[the_array objectAtIndex:i] intValue]);
}
For the CGRect example:
for (int i = 0; i < max; i++)
{
CGRect temp_rect = [[the_array objectAtIndex:i] CGRectValue];
NSLog(#"%i: x:%f y:%f w:%f h:%f", i, temp_rect.origin.x, temp_rect.origin.y, temp_rect.size.width, temp_rect.size.height);
}
In a nutshell, you are storing objects not integers in your code. You have to pull them out of there as objects and then extract your integer to get your data back.
Just found the answer in another question I posed:
it must be:
btn.tag = [[currentNoteBook.tabReference objectAtIndex:k] intValue];