开发者

randomising questions

开发者 https://www.devze.com 2023-01-16 13:03 出处:网络
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 开发者_Python百

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 开发者_Python百科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


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?


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...


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); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号