0

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

Alex Stelea
  • 1,219
  • 2
  • 18
  • 29
  • If 'question' is an 'NSInteger', I don't think that's the line that's causing the crash... I could always be wrong, though. – filipe Sep 15 '10 at 20:33
  • the actual arc4random doesnt crash something in the statment gives and EXC_BAD_ACCESS error after i run the app for a while. – Alex Stelea Sep 15 '10 at 20:39
  • Are you using *question* as the index into a 7-element array of questions? If so, that's your problem: The index of the first element in an array is 0, and the index of the 7th element is 6. You're generating numbers from 1 to 7, so whenever you try and access the element with index 7, you're asking for the *8th* element. This will cause an illegal access exception. – user359996 Oct 10 '10 at 00:10

3 Answers3

0

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?

Leo Cassarani
  • 1,289
  • 10
  • 14
  • 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) – Alex Stelea Sep 15 '10 at 20:36
0

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
  • 74,769
  • 26
  • 128
  • 150
0

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); 
}
Community
  • 1
  • 1
J Edward Ellis
  • 1,368
  • 2
  • 12
  • 21
  • 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 – Alex Stelea Sep 16 '10 at 05:22
  • can u please clarify what the i=o, i<3 does? – Alex Stelea Sep 18 '10 at 18:48
  • 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 Sep 24 '10 at 21:55