Database random cell help.

Discussion in 'iOS Development' started by papaluchi, May 5, 2009.

  1. papaluchi

    papaluchi New Member

    Joined:
    May 5, 2009
    Messages:
    1
    Likes Received:
    0
    I'm working on an app that is just getting to big for code and I want to use a database. How can I make my code grab a random cell from my databased based on the random number it generates as opposed to getting the case resulting in the random number?


    - (IBAction)jokes {
    int rNumber = arc4random() % 5;

    switch (rNumber) {
    case 0:
    result.text = @"A.";
    break;
    case 1:
    result.text = @"B";
    break;
    case 2:
    result.text = @"C";
    break;
    case 3:
    result.text = @"D";
    break;
    default:
    break;

    }
    }
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    first of all, int rNumber = arc4random() % 5; only generates a pseudo random number. Secondly, you're creating a larger spread than you allot for in your switch.

    Basically (and I don't know who started this arc4random() kick), you'll want to see a random number generator and generate a random number that way. It's impossible in code to truly create a random number, so it is generally recommended that you seed the random number generator with the system time.

    Once you have your random number generator seeded, just mod the result by whatever, and set that as an int, which you will use to pull data from your database.

Share This Page