How to display the array values in random manner

Go To StackoverFlow.com

-3

Possible Duplicate:
What's the Best Way to Shuffle an NSMutableArray?

iam developing one application.In that i have one array.That contain 5 values 1,2,3,4,5.ANd i want to display that values in random manner.For example first time it display 3 2 4 5 1.And second time 1 3 2 4 5.Like this every time i want to change the order.So please tell me how to do this one.

2012-04-04 04:25
by Venkat1282
duplicate : http://stackoverflow.com/questions/7047085/reading-random-values-from-an-array, http://stackoverflow.com/questions/56648/whats-the-best-way-to-shuffle-an-nsmutablearray, http://stackoverflow.com/questions/4202102/iphone-nsarray-nsmutablearray-re-arrange-in-random-orde - Sarah 2012-04-04 04:28
Please learn to search. A simple lookup of "random nsarray" would yield you many results - sudo rm -rf 2012-04-04 04:30
use rand(), random() or arc4random() functio - Hiren 2012-04-04 04:36
answers are everywhere, just learn to find them : - Bazinga 2012-04-04 07:31


1

  1. you can generate a random number between 0 and array size -1
  2. use the number to index the array
  3. remove item at the array index

sample code:

int i = arc4random() % 4;
NSString value = [arr objectAtIndex: i]
[arr removeObjectAtIndex: i];

repeat the steps till you have an empty array.

2012-04-04 04:37
by Charlie Wu
Ads