reading an array - iphone

I create random numbers using the following code and store them in an array.
NSMutableSet *aSet = [NSMutableSet setWithCapacity:6];
while([aSet count]<=6){
int Randnum = arc4random() % 12;
[aSet addObject:[NSNumber numberWithInt:Randnum]];
}
NSArray *arrayOfUniqueRandomNumbers = [aSet allObjects];
Now, I need to read the array to get the values one-by-one using a forloop like
for (int i = 0; i<6; i++);
Can anyone please help me to finish the code?

You can do:
for (NSNumber *val in arrayOfUniqueRandomNumbers) {
int i = [val intValue];
...
}

Related

Random number generation from an NSMutable array [duplicate]

This question already has answers here:
Non repeating random numbers in Objective-C
(6 answers)
Generating non-repeating random numbers
(5 answers)
Closed 9 years ago.
I need to generate random numbers from an array i have tried this inside the for loop
rnd = arc4random_uniform(arr.count);
it generated random numbers but some numbers get repeated also tried Random() in math.h
still the same problem persists please help me out..
Thanks in advance..
if i understood
"I need to generate random numbers from an array"
correctly, you want the numbers to be taken from an array randomly, if so then
first store the numbers in an NSMutableArray
NSMutableArray *arr=//store your numbers in this array.
-(void)getRandomNumberFromArray:(NSMutableArray *)arr{
int r = arc4random() % arr.count;
int number=[arr objectAtIndex:r];
[arr removeObjectAtIndex:r];
}
Generate a random number for each element, and then check to make sure it's not the same as one of the existing ones. If it already exists, try again.
for(int i = 0; i < arr.count;i++)
{
BOOL isRandom = YES;
int rand = -1;
while(!isRandom )
(
isRandom = YES;
rand = arc4random() % 5;
for(int j = 0; j < i; j++)
{
int existingNumber = arr[j];
if(existingNumber == rand)
{
isRandom = NO;
break;
}
}
}
arr[i] = rand;
}
Another option is to first just assign them to have incrementing values, and then shuffle the mutable array. What's the Best Way to Shuffle an NSMutableArray?
If I understood your problem , then you dont need random numbers , you need to shuffle the elements in the Array ?
Then you need to use this method ,
-(void)shuffleWithArray:(NSArray*)cardsArray
{
NSMutableArray *shuffleArray =[[NSMutableArray alloc]initWithArray:cardsArray];
// NSUInteger count1=[shuffleArray count];
for (NSUInteger i= 0; i<[shuffleArray count]; i++)
{
int nElement=[shuffleArray count] -i;
int n=(arc4random()%nElement + i);
[shuffleArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
}
You can make use of the following function.
#property (nonatomic,strong) NSMutableArray * numbers;
- (NSInteger) nonRepeatedNumber
{
if(_numbers == nil)
{
_numbers = [[NSMutableArray alloc]init];
}
NSInteger number = arc4random()% arr.count;
while ([_numbers containsObject:[NSNumber numberWithInt:number]])
{
number = arc4random()% arr.count;;
}
[_numbers addObject:[NSNumber numberWithInt:number]];
return number;
}
Check the below Tutorial. it must help for you.
iOS Random Number generator
Try the following method
-(NSMutableArray*)randomNumbersfromArray:(NSArray*)array {
NSArray *numbers = array;
int count = [numbers count];
NSMutableArray *addedIndexes = [[NSMutableArray alloc]initWithCapacity:count];
NSMutableArray *addedObjects = [[NSMutableArray alloc]initWithCapacity:count];
int i = 0;
while ([addedIndexes count] != [numbers count]) {
int random = arc4random() % count ;
if (![addedIndexes containsObject:[NSString stringWithFormat:#"%i",random]]) {
[addedIndexes addObject:[NSString stringWithFormat:#"%i",random]];
[addedObjects addObject:[numbers objectAtIndex:random]];
i++;
}
}
return addedObjects;
}
#include <stdlib.h>
int r = 0;
if (arc4random_uniform != NULL)
r = arc4random_uniform (arr.count);
else
r = (arc4random() % arr.count);
int randomNumberFromArray=[arr objectAtIndex:r];
EDIT
Bingo.After some thinking i made it working
-(NSArray *)randomizeArray:(NSArray *)inputArray
{
NSMutableArray *checkArray=[[NSMutableArray alloc]initWithCapacity:3];
NSMutableArray *outputArray=[[NSMutableArray alloc]initWithCapacity:3];
while ([outputArray count]<[inputArray count])
{
int r=[self getRandomNumber:[inputArray count]];
if ([checkArray containsObject:[NSNumber numberWithInt:r]])
{
}
else
{
[checkArray addObject:[NSNumber numberWithInt:r]];
[outputArray addObject:[inputArray objectAtIndex:r]];
}
}
return outputArray;
}
-(int)getRandomNumber:(int)maxValue
{
int r = 0;
if (arc4random_uniform != NULL)
r = arc4random_uniform (maxValue);
else
r = (arc4random() % maxValue);
return r;
}
This randomizeArray: method will give you the whole array randomized

NSMutableArray integer storage

I am trying to store numbers inside the NSMutableArray, and have wrapped them like so:
-(IBAction)opUp:(id)sender{
opBool = YES;
opNum = [sender tag];
[numArray addObject:[NSNumber numberWithLong:number2]];
[opArray addObject:[NSNumber numberWithInt:opNum]];
number1 = 0;
number2 = 0;
opNum = 0;
NSLog(#"Operand %i", opNum);
NSLog(#"%i, %i", numArray.count, opArray.count);
}
But when I call them later using this:
final2 = [numArray objectAtIndex:i];
operthing = [opArray objectAtIndex:i];
I get this error message:
operthing equals 111526320
'operthing' is not supposed to go beyond 20.
Can someone help me with this?
Thanks.
operthing = [[opArray objectAtIndex:i] intValue];

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];

How to add numbers in a mutable array in iPhone?

I am new to iPhone development. I want a Nsmutable array to hold numbers from 1 to 100. How can I do it? How can I implement in a for loop? Is there any other way to hold numbers in array in iPhone?
You can only add NSObject subclasses in Cocoa containers. In your case, you will have to wrap your integers in NSNumber objects:
NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
[array addObject:[NSNumber numberWithInt:i]];
}
To extract the values:
int firstValue = [[array objectAtIndex:0] intValue];
Use an NSNumber object:
[NSNumber numberWithInt:1];
The short hand solution
NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
[array addObject:#(i)];
}
int intValue = 10;
NSNumber *numberObj = #(intValue);

How to check repetition of numbers in an array?

I generate random numbers and store them in an array:
int RandomNumber = arc4random() % 12;
[NSMutablearray *Number addObject:[NSNumber numberWithInt:RandomNumber]];
Now I want to make sure the same number is not created randomly again.
Can any one please tell me how to do it with sample code.
You can use an NSMutableSet instead of an array while generating the numbers.
The following code will create an array of 10 unique random numbers:
NSMutableSet *aSet = [NSMutableSet setWithCapacity:10];
while([aSet count]<=10){
int Randnum = arc4random() % 12;
[aSet addObject:[NSNumber numberWithInt:Randnum]];
}
NSArray *arrayOfUniqueRandomNumbers = [aSet allObjects];