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 ?
Asked
Active
Viewed 1,598 times
1
-
1Just 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 Answers
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