tags:

views:

52

answers:

3

i am wondering how i can randomise the order of questions when i have question as an NSInteger i am using the following line of code to try and display my arc4random however it crashes and it doesnt load properly.

    question = arc4random() %6 +1;

is theere a solution to get the integer question to be randomized in its order? thanks

A: 

I'm not entirely sure I understand what you're trying to do, but have you definitely added the line #include <stdlib.h> to the top of your .m file?

L Cassarani
i was missing this that solved the error however i tried replacing my %6 with %question which is also an nsinteger however this doesnt run properly. (again crashes)
Alx
A: 

Why do you have "+1". Almost certainly you are getting a random number outside of the range of your array, which would seem to be six items long...

Kendall Helmstetter Gelner
A: 

Your question appears to be about a crash in your program which displays questions in a random order. The only line of code you provide calculates a random number 1-7 inclusive. There is nothing wrong with that line of code.

In a later comment you say that your application crashes with the EXC_BAD_ACCESS error. This question about the EXC_BAD_ACCESS error indicates that you are crashing from an illegal memory access. If you want help on this problem, I suggest that you post more code because the error is somewhere else.

Are you looking for something like this?

for(int i=0; i<3; i++) { 
   question = arc4random() %6 +1; 
   DisplayQuestion(question); 
}
J Edward Ellis
This is a problem howeverthe code I'm trying to randomize is supposed to select a random question from 1 to 7, however it doesn't really display the correct amount of questions and usually crashes after the first question. Is there a way I can specify this code to randomize for 3 distict randoms from 1-7
Alx
can u please clarify what the i=o, i<3 does?
Alx
This is a for loop. It is standard C and Objective C notation. It means start with i = 0 and increment i by one each loop as long as i is less than 3. That way i take three values 0, 1, and 2. You said you wanted to display three questions.
J Edward Ellis