0

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;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • just assign the first one yourself and generate the rest of the random numbers. No need for a while loop – Bushra Shahid Aug 09 '11 at 09:20
  • http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c – Akshay Aug 09 '11 at 09:21
  • If the expected output is always same then how could it be random number? May be I missed something in the question. – taskinoor Aug 09 '11 at 09:21
  • its not the same, i gave an example,the first number is set by me and the other numbers by the method ,and every number is shown only one time. – YosiFZ Aug 09 '11 at 09:26
  • 2
    It looks like you're basically after something that shuffles. See the [tag:shuffle] tag for some ideas. – martin clayton Aug 09 '11 at 10:29

2 Answers2

4

int r = arc4random() % 9 + 5 will give you numbers between 5 and 13 including both of them.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
alloc_iNit
  • 5,173
  • 2
  • 26
  • 54
3

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:

  1. Populate the array with all numbers between minValue and maxValue (both included) except for firstValue.

  2. Shuffle the array.

  3. 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)
albertamg
  • 28,492
  • 6
  • 64
  • 71