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; } } 
5
  • just assign the first one yourself and generate the rest of the random numbers. No need for a while loop Commented Aug 9, 2011 at 9:20
  • stackoverflow.com/questions/160890/… Commented Aug 9, 2011 at 9:21
  • If the expected output is always same then how could it be random number? May be I missed something in the question. Commented Aug 9, 2011 at 9: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. Commented Aug 9, 2011 at 9:26
  • 2
    It looks like you're basically after something that shuffles. See the shuffle tag for some ideas. Commented Aug 9, 2011 at 10:29

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Why would someone downrate this? It's the best possible answer.
@Alex K.: Same thing I also want to know as it's a puzzle for me...Thanks for your concern.
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.