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];
Related
I want to make a question app which shows a random question from a plist I made. This is the function (there are only 7 questions for now).
My function gives a random question but it always starts with the same question
and a question can be repeated. I need your help to generate the question randomly and without repetition.
currentQuestion=rand()%7;
NSDictionary *nextQuestion = [self.questions objectAtIndex:currentQuestion];
self.answer = [nextQuestion objectForKey:#"questionAnswer"];
self.qlabel.text = [nextQuestion objectForKey:#"questionTitle"];
self.lanswer1.text = [nextQuestion objectForKey:#"A"];
self.lanswer2.text = [nextQuestion objectForKey:#"B"];
self.lanswer3.text = [nextQuestion objectForKey:#"C"];
self.lanswer4.text = [nextQuestion objectForKey:#"D"];
rand()%7; will always produces a unique sequence of random numbers.
Use arc4random() % 7; instead.
currentQuestion=arc4random() %7;
I'd do it this way (in ARC, written out extra long for clarity):
#property (nonatomic,strong) NSDictionary *unaskedQuestions;
- (NSString *)nextRandomUnaskedQuestion {
if (!self.unaskedQuestions) {
// using your var name 'nextQuestion'. consider renaming it to 'questions'
self.unaskedQuestions = [nextQuestion mutableCopy];
}
if ([self.unaskedQuestions count] == 0) return nil; // we asked everything
NSArray *keys = [self.unaskedQuestions allKeys];
NSInteger randomIndex = arc4random() % [allKeys count];
NSString *randomKey = [keys objectAtIndex:randomIndex];
NSString *nextRandomUnaskedQuestion = [self.unaskedQuestions valueForKey:randomKey];
[self.unaskedQuestions removeObjectForKey:randomKey];
return nextRandomUnaskedQuestion;
}
Use an array of your question keys. Say you have array named arrKeys --> [A], [B], [C], [D], ... , [z], nil
Use (arc4random() % array.length-1) {as suggested by Suresh} to generate rendom index for your array. Say you got rand = 3
Get the key from array arrKeys #3 = D. Then from your NSDictionary use [nextQuestion objectForKey:#"D"] and also remove the 'D' key from your array as [arrKeys removeObjectAtIndex:3]. Repeat 1-3 steps for next question.
I have NSMutableArray having values 10,15,26,28. I want to store them into another array as an integer form how do i store them.
Thanks :)
here how to add
[array addObject:[NSNumber numberWithInt:10]];
you can get the integer value like
//assuming array has nsnumber/nsstring objects.
[[array objectAtIndex:index] intValue];
Here's an example of how to do this:
int yourInt = 5;
[myMutableArray addObject:[NSNumber numberWithInt:yourInt]];
You can't store C types in a NSMutableArray, you can only store objects.
Create NSNumber's from your int values with [NSNumber numberWithInteger:10];...
You can then get the int value back with [aNSNumberObject intValue];
If you want to store integers in an array it has to be a C array:
#define C_ARRAY_MAX_SIZE 10
int cTab[C_ARRAY_MAX_SIZE];
int i=0;
for (NSNumber* n in yourMutableArray) {
cTab[i] = [n intValue];
i++;
}
When the user click the next button, it generate random number and I would like to store the number into the array. My array is storing the last number only. Should I initialize the array outside the 'next' function?
Moreover, I would like the 'back button' to read the array from the last in number.
Please advise.
- (IBAction)Next:(id)sender {
//generate random number - result is a range of 0-10
int randomnumber = (arc4random() % 9);
//hold the array number up to 10
NSMutableArray *myArray = [NSMutableArray arrayWithCapacity:10];
// insert the random number into array
[myArray addObject:[NSNumber numberWithInt:randomnumber]];
// when I see in debug mode, my array only storing the last number
NSLog([myArray description]);
}
- (IBAction)Back:(id)sender {
//I miss a lot of code in this part
NSNumber *last_array_num = [myArray objectAtIndex:0];
}
Whenever the function next called the NSMutable array is declared again and again... so try declare the NSMutable array outside the next function
You should initialize array as a class variable.
the randomnumber will be in the 0-8 range since it can't be grater than 9 cause that wouldn't be module of it.
Also your problem is you're creating a new array each time
You should create it outside and use a instance variable.
- (id)init {
myArray = [NSMutableArray arrayWithCapacity:10];
[super init];
}
define in your header file the myArray as a Mutable Array
Try this,
- (void) initArray
{
//hold the array number up to 10
myArray = [NSMutableArray arrayWithCapacity:10];
}
- (IBAction)Next:(id)sender {
//generate random number - result is a range of 0-10
int randomnumber = (arc4random() % 9);
// insert the random number into array
[myArray addObject:[NSNumber numberWithInt:randomnumber]];
// when I see in debug mode, my array only storing the last number
NSLog([myArray description]);
}
- (IBAction)Back:(id)sender {
//I miss a lot of code in this part
NSNumber *last_array_num = [myArray objectAtIndex:[myArray.count - 1]];
}
I have NSInteger variable, for example NSInteger example=1256 and i need an array with elements of this variable.
so first element of array is array[0] = 1
array[1] = 2
array[2] = 5 etc..
what way can i solve it ?
Here's about how I'd do it:
NSUInteger number = 1234567890;
NSMutableArray * numbers = [NSMutableArray array];
while (number > 0) {
NSUInteger lastDigit = number % 10;
[numbers insertObject:[NSNumber numberWithUnsignedInteger:lastDigit] atIndex:0];
number = number / 10;
}
You need to use NSMutableArray to be able to change entries. NSMutableArray can only hold objects, not primitive types like NSInteger. Also, if you are using NSMutableArray, you can't access the elements the same way as with a C array.
[array insertObject:[NSNumber numberWithInteger:2] atIndex:1];
You can convert your integer to a char* then iterate through it casting each character back to an int and adding it to a C array or, as Steven says, an NSArray of NSNumbers.
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];
...
}