fill array with random numbers different from each others? [duplicate] - ios

This question already has answers here:
List of random numbers - arc4random
(2 answers)
Closed 9 years ago.
I would like to have an array with 12 numbers -> 0 to 11
the array must be random, and i don't want to have twice the same number
thanx

Sounds like a shuffling problem.
Just declare an array like follows
NSMutableArray * numbers = [NSMutableArray array];
for (int i = 0; i < 12; i++) {
[numbers addObject:#i];
}
Then you can shuffle that array using the Fisher-Yates algorithm
for (NSUInteger i = numbers.count - 1; i > 0; --i) {
NSUInteger n = arc4random_uniform(i+1);
[numbers exchangeObjectAtIndex:i withObjectAtIndex:n];
}

I suggest you create an array and fill it in a loop with the numbers 0 to 11. In a second step, you shuffle that array: What's the Best Way to Shuffle an NSMutableArray?

You could try something like:
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++) {
int randomNumber = min + rand() % (max-min);
[array addObject:[NSNumber numberWithInt:randomNumber]];
}
Not sure if the syntax is correct, im on a Windows machine now

Related

NSNumber in arrays, ios

I am trying to learn about how to put numbers into an array with nsnumber. The exact thing I'm stuck with is, To build the sequence in the array, we're going to need a loop. Between creating the sequence array and returning it, declare a for loop whose counter is limited by index + 1 and increments by one.
Since the sequence requires the two previous numbers to calculate the next one, we need to prime the sequence. We're going to need to manually pass in #0 and #1 on the first two iterations of the loop. This is what I have so far.
(NSArray *)arrayWithFibonacciSequenceToIndex:(NSUInteger)index
{
NSMutableArray *sequence = [NSMutableArray array];
for(NSUInteger i = 0; i < 1; i++)
{
index = i+1;
}
return sequence;
}
Am I on the right track? I'm not sure if my for loop is correct. Do I put sequence into the for loop and add the nsnumber #0 and #1 there or do I put those numbers into the sequence outside the loop?
To insert a number in an NSArray, you have to wrap them in a NSNumber:
NSInteger a = 5;
NSNumber number = #(a); // ou #5;
to perform mathematical operations on 2 NSNumbers, you have to convert them to integer (or double, float...) before
NSNumber * number1 = #1;
NSNumber * number2 = #6;
NSInteger sum = [number1 integerValue] + [number2 integerValue];
for the fib problem, youre loop is correct. The way I would think of this is : I add my value in the for loop, and if I'm adding the 1st or 2nd element, then I put a 0, else I sum the last 2 elements:
- (NSArray *) fibbonacciSequenceWithSize:(NSInteger)size
{
NSMutableArray * result = [NSMutableArray new];
for(NSInteger idx = 0; i < size ; i ++)
{
// first 2 numbers of fib sequence are 1
if(idx == 0 || idx == 1)
{
[result addObject:#1];
}
else
{
// Add the 2 previous number
// F2 = F1 + F0
NSinteger next = [result[idx - 2] integerValue] + [result[idx - 1] integerValue];
[result addObject:#(next)];
}
}
return [result copy]; // copy the NSMutableArray in a NSArray
}
You can clean up the code by having a Fibonacci function that provides the sum of the last two elements.
- (NSNumber *)nextFibInArray:(NSArray *)array {
if (array.count < 2) return #1;
NSInteger lastIndex = array.count - 1;
return #([array[lastIndex-1] intValue] + [array[lastIndex] intValue]);
}
Then the loop is cleaner, too.
- (NSArray *)fibonacciWithLength:(NSInteger)length {
NSMutableArray *result = [#[] mutableCopy];
for (NSInteger i=0; i<length; i++) {
[result addObject:[self nextFibInArray:result]];
}
return result;
}
We could trim some execution time fat from this, but for short enough sequences, this should be clear and quick enough.

Sorting through an array objects to find the highest number [duplicate]

This question already has answers here:
Comparing NSNumbers in Objective C
(5 answers)
Closed 8 years ago.
I'm trying to sort through an array of objects to find the highest number. I'm fairly new with objective C (and C in general), so I am not sure why this isn't working, although my gut says its probably due to the NSNumber declaration. The max variable is always returned as random value from the array. Any ideas?
NSNumber * max = 0;
for (int i=0; i<mutableChartData.count; i++) {
if ([mutableChartData objectAtIndex:i] > max) {
max =[mutableChartData objectAtIndex:i];
}
}
enumerate array using code below:
NSNumber *max = 0;
[mutableChartData enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *Stop)
{
if ([mutableChartData[i] intValue] > [max intValue]) {
max =mutableChartData[i];
}
}];
enumerartion is fast and efficient and you will get maximum number in array

How to prevent duplication when picking objects in array from random indexes [duplicate]

This question already has answers here:
Getting a random object from NSArray without duplication
(3 answers)
Closed 8 years ago.
Hi I am picking objects from one array and adding it into another. Using the below method
I am doing this in a for loop.
FirstArray = [#"object1",#"object2",#"object3",#"object4",#"object5"];
SecondArray = [#"abc",#"pqr",#"xyz",#"123"];
NSUInteger FirstArrayIndex = arc4random() % [FirstArray count];
NSUInteger SecondArrayIndex = arc4random() % [SecondArray count];
[mainArray addObject:[FirstArray objectAtIndex:FirstArrayIndex]];
[mainArray addObject:[SecondArray objectAtIndex:SecondArrayIndex]];
But when I am using this - some times the objects will get duplicated in mainArray. How to prevent this ?
Without saving the index, is there any other method to achieve this ?
Check if it exists in the destination array before adding it:
MyArray = [#"object1",#"object2",#"object3",#"object4",#"object5"];
for (NSUInteger i = 0; i < 3; i++) { // Assuming you are adding 3 objects
NSUInteger RandomIndex;
do {
RandomIndex = arc4random() % [MyArray count];
} while ([mainArray indexOfObject:[MyArray objectAtIndex:RandomIndex]] == NSNotFound)
[mainArray addObject:[MyArray objectAtIndex:RandomIndex]];
}
If you are just trying to randomize the array you use this method which this answer provides https://stackoverflow.com/a/56656/1916721:
NSUInteger count = [MyArray count];
for (NSUInteger i = 0; i < count; ++i) {
NSInteger remainingCount = count - i;
NSInteger exchangeIndex = i + arc4random_uniform(remainingCount);
[MyArray exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
}
You could then just pull elements one by one out of the shuffled array or copy it entirely.
The alternative would be to just check if it already exists, otherwise find another random index.
You should remove the object that you are selecting from the initial array so that you do not pick it again.

Quiz application - How can I choose 3 wrong answers to show from an array of 6 wrong answers?

I am trying to make a quiz app and I am new to IOS.I want to get 3 wrong answers from an array of 6 wrong answers for a specific question and there will be 1 correct answer for each question.I want to randomize the wrong options every time the question is shown.So every time the question is shown the app will choose 3 wrong options from the array and 1 correct answer.How can I code this?I would be glad if you can help me on this.
Thank you..
{
int counted = [theOptions count];
int i;
NSMutableArray *indexes = [[NSMutableArray alloc] initWithCapacity:counted];
for (i=0; i<counted; i++) [indexes addObject:[NSNumber numberWithInt:i]];
NSMutableArray *shuffle = [[NSMutableArray alloc] initWithCapacity:counted];
while ([indexes count])
{
int index = rand()%[indexes count];
[shuffle addObject:[indexes objectAtIndex:index]];
[indexes removeObjectAtIndex:index];
}
NSMutableArray* shuffledOptions = [[NSMutableArray alloc] initWithCapacity:4];
for (int i=0; i<counted; i++)
{
int randomIndex = [[shuffle objectAtIndex:i] intValue];
[shuffledOptions addObject:[theOptions objectAtIndex:randomIndex]];
UIButton* optionButton = [_optionsButtonsArray objectAtIndex:i];
optionButton.tag = randomIndex;
}
return shuffledOptions;
}
return theOptions;}
If you have an array of 6 wrong answers and want to randomly select three of them
NSArray *answers = [NSArray arrayWithObjects:#"Wrong #1", #"Wrong #2", #"Wrong #3", #"Wrong #4", #"Wrong #5", #"Wrong #6"];
NSMutableArray *output = [[NSMutableArray alloc] init];
int r;
while (output.count < 3) {
r = arc4random_uniform(5);
if (![output containsObject:[answers objectAtIndex:r]]) {
[output addObject:[answers objectAtIndex:r]];
}}
The easiest way would be select a random number between 0-5 then select another random one. If the next one is a duplicate of the first one repeat the previous step. Do this one more time and you now have three unique random numbers from 0-5.
A more clever way would be to store numbers 1-5 in an array. Shuffle the array and then return the first three indexes.
Good luck.

Random number in Objective-C [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Generating random numbers in Objective-C
iOS Random Numbers in a range
Generate non-repeating, no sequential numbers
I am looking for method that give me random numbers between two numbers, and the first number will be number that I choose between the numbers.
for example, if I give this random function 5 and 10 and 9 as the first number, so it will give me: 9,10,7,6,5,8
I tried to use it:
NSUInteger count = [array count];
for (NSUInteger i = 1; i < count; ++i) {
int nElements = count - i;
int n = (random() % nElements) + i;
while (n==`firstnumber`) {
n = (random() % nElements) + i;
}
}
int r = arc4random() % 9 + 5 will give you numbers between 5 and 13 including both of them.
the first number is set by me and the other numbers by the method ,and
every number is shown only one time
It looks like you are after a shuffling algorithm. The following category on NSMutableArray will do the job:
#interface NSMutableArray (Shuffling)
- (void)shuffle;
#end
#implementation NSMutableArray (Shuffling)
- (void)shuffle
{
// Fisher–Yates shuffle (modern algorithm)
// To shuffle an array a of n elements (indexes 0..n-1):
// for i from n − 1 downto 1 do
// j <-- random integer with 0 <= j <= i
// exchange a[j] and a[i]
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
for (int i = [self count] - 1; i >= 1; i--) {
int j = arc4random() % (i + 1);
[self exchangeObjectAtIndex:j withObjectAtIndex:i];
}
}
#end
Your requirement is that the number in the first position of the array is fixed (given by you). Then, you can do something like this:
Populate the array with all numbers between minValue and maxValue (both included) except for firstValue.
Shuffle the array.
Insert firstValue at the first position in the array.
Resulting in the following code:
NSInteger minValue = 5;
NSInteger maxValue = 10;
NSInteger firstValue = 9;
// minValue <= firstValue <= maxValue
// populate the array with all numbers between minValue
// and maxValue (both included) except for firstValue
NSMutableArray *ary = [NSMutableArray array];
for (int i = minValue; i < firstValue; i++) {
[ary addObject:[NSNumber numberWithInt:i]];
}
for (int i = firstValue + 1; i <= maxValue; i++) {
[ary addObject:[NSNumber numberWithInt:i]];
}
// --> (5,6,7,8,10)
// shuffle the array using the category method above
[ary shuffle];
// insert firstValue at the first position in the array
[ary insertObject:[NSNumber numberWithInt:firstValue] atIndex:0];
// --> (9,x,x,x,x,x)

Resources