How to randomize the random() function?

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

  1. midntdj macrumors newbie

    Joined:
    Sep 23, 2009
    #1
    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?

    Code:
    
    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

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
  4. midntdj thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #4
    Using srandom did the trick.

    Thanks for posting the other related link.
     

Share This Page