How to randomize the random() function?

Discussion in 'iOS Programming' started by midntdj, Sep 23, 2009.

  1. midntdj macrumors newbie

    Sep 23, 2009
    I have a routine to randomly pick an item from a NSArray using

    random() % 9

    where 9 is the number of items in the NSArray. The problem I'm seeing is that every time I start my application, I'm getting the exact same random numbers being generated. This only happens when I exit the application and than restart it. I was expecting to see a different pattern each time I start the app. Is there a way to randomize the random function?

    NSString strTemp = [[ NSArray arrayWithObjects: @"a.png", @"b.png", @"c.png", 
                               @"d.png",  @"e.png", @"f.png", @"h.png",@"i.png",
                               @"j.png",nil] objectAtIndex: (random() % 9)];

    -- Thanks
  2. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
  3. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
  4. midntdj thread starter macrumors newbie

    Sep 23, 2009
    Using srandom did the trick.

    Thanks for posting the other related link.

Share This Page