0

I know it's newbie question but I really need some help. I am trying to create a while loop with an integer temporary array on Xcode for four numbers. On every loop I want to check the previous number and store it to my table until that fills the four numbers.The numbers that I want to randomize are between 2-5.

So here is my thought for the start:

ans= arc4random()%4+2;

            seq[]=ans;
            counter=+1
            while (uniq==true) {
                ans=arc4random()%4+2;
            }
            Uniq=true
            for(int i=0; I <4; i++)
            If (ans=seq[i]){
            Uniq==false

            Seq[counter=ans]
                Counter++}

I know that i am missing things. If someone can help me i will be gratefull

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
user2437643
  • 1
  • 1
  • 1
  • 2,3,4,5 is the list from where you want random picks? – Anoop Vaidya May 30 '13 at 17:50
  • Yes, that's why i start with ans= arc4random()%4+2; and then i want to check the first result and then continue with next number of these numbers until it fill the whole array. – user2437643 May 30 '13 at 17:55
  • 1
    @user2437643 What are you actually trying to accomplish here? If you want the numbers 2-5 in a random order, it would be much more efficient to shuffle them. – pjs May 30 '13 at 18:06
  • I am trying to create random pick of 2-5 numbers and finally to have an array let say as example seq=[3,5,2,4]. And i need this result to put it on 4 labels that i created. – user2437643 May 30 '13 at 18:12
  • 1
    Just throw them in a NSMutableArray and shuffle them using this: http://stackoverflow.com/a/56656/1445366 – Aaron Brager May 30 '13 at 18:19
  • 1
    I can't believe no one has marked this dupe yet -- this question comes up about once every two weeks. – Hot Licks May 30 '13 at 18:19
  • Sorry, @HotLicks, I've been elsewhere. :) Possible duplicate of [Non-repeating random numbers](http://stackoverflow.com/q/1617630), and [a veritable host of others](http://stackoverflow.com/search?q=%5Bobjc%5D+non-repeating+random+numbers). – jscs May 30 '13 at 20:41

1 Answers1

1

Try this one: (*Not compile checked)

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"2", @"3", @"4", @"5"]];
NSMutableArray *mArray = [NSMutableArray new];

while (array.count > 0) {
    NSUInteger index = arc4random() % array.count;
    mArray[mArray.count] = array[index];
    [array removeObjectAtIndex:index];
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Corrected the obvious errors, not sure if you can add items with `[]` since I don't use it. – Sulthan May 30 '13 at 18:21
  • @Sulthan: you can add :) if the index is out of bound a new one gets added. `mArray[mArray.count]` was I wanted to use. even your edit is good, same thing :). – Anoop Vaidya May 30 '13 at 18:23
  • Oh, sorry. The `.` was missing so I just chose one solution :) – Sulthan May 30 '13 at 18:27
  • Thank you for this response. And from this mArray how can i pull out the random series on labels? On my previus code i used standard the numbers like this. answer1.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)]; – user2437643 May 30 '13 at 19:14
  • @user2437643: marray contains the random series, you can get values from this array. – Anoop Vaidya May 31 '13 at 04:24