create random integers for quiz using plist

前端 未结 1 1858
既然无缘
既然无缘 2021-01-25 12:29

I would like to have user click on a button to generate a ten-question quiz in the form of \"a +/- b = c\" where the values for a and b are from +10 to -10 and are randomly assi

相关标签:
1条回答
  • 2021-01-25 13:17

    Instead of using a NSDictionary, use NSArray if your keys are just numbers. You could then do

    NSString *randomString = [array objectAtIndex:(arc4random() % [array count])]; 
    

    to pick a random element.

    However, I would really advice against looking it up in a plist if it's just different combinations of random numbers. Writing out all those combinations by hand is just a waste of time. That's what computers are for!

    Old, but still relevant answer:

    To generate a random number between -10and 10:

    int a = (arc4random() % 21) - 10;
    

    You could also make a function like this:

    int randomIntegerInRange(int min, int max)
    {
        int range = max - min + 1;
        return min + arc4random() % range;
    }
    
    0 讨论(0)
提交回复
热议问题