1

I have created a quiz application. I want to generate a random question. The questions come from Core Data. When view didAppear is called I store the question in an array and display it by a display question method. But i want to display the questions randomly without repeating any question, and every question will display. How can I do this ?

Paul Cezanne
  • 8,629
  • 7
  • 59
  • 90
dulal_026
  • 589
  • 1
  • 6
  • 20
  • 1
    Just trying to get a feel for your post here...How are the questions stored in Core Data? Is there a way you can easily say "I want question X"? – SomeRandomGuy Apr 27 '11 at 17:44
  • I would create a random sequence of `0..n-1` (it represents the indices of the `n` questions), and I would read the questions by that order of the indices from the database sequentially; and you won't run into a situation when a question is shown twice as every index exists only once; that looks the most straightforward solution to me; it can be achieved in a minute. – holex Oct 16 '15 at 15:32

2 Answers2

0

You need to shuffle the array. Look here:

canonical way to randomize an NSArray in Objective C

Community
  • 1
  • 1
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
0

You should copy your questions in an tmp array and each time a question is randomly picked by a random function (eg : int r = arc4random() % [myArray count];) you delete its entrance from the tmp array.

jlink
  • 682
  • 7
  • 24