Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 974653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:28:01+00:00 2026-05-16T03:28:01+00:00

Every now and again, the following comes out as -1 int randomIndex = random()

  • 0

Every now and again, the following comes out as -1

int randomIndex = random() % [directionsArray count] - 1;

The init method for this class is where directionsArray is created and srand() is called. It looks like this

- (id) initSprite
{
    if ( ( self = [super initSprite] ) ) {

        speed = 1;

        directionsArray = [[NSArray arrayWithObjects:STRING_CONSTANT, STRING_CONSTANT, nil] retain];
        srand(time(NULL));
        [self pickRandomDirection];


    }
    return self;
}

- (void) pickRandomDirection
{
    int randomIndex = random() % [directionsArray count] - 1; // This sometimes comes out as -1??
}

At the moment, I’m working around it by using abs(randomIndex), but that’s cheating and I should probably know what’s going on for future reference.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:28:02+00:00Added an answer on May 16, 2026 at 3:28 am

    Because 0 – 1 equals -1.

        random() % [directionsArray count] - 1;
    

    What if random() returns 0 here ? You get 0%[directionsArray count] – 1; which is -1, since % has precedence over –

    Perhaps you want random() % ([directionsArray count] - 1);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every now and again, I need to start the Django development server, and have
I'm running Django on Linux using fcgi and Lighttpd. Every now and again (about
Every now and then Elastic Load Balancer (ELB) kicks out one of my servers
I'm finding this problem every now and then in my production website, and it
I am learning Java (slowly) from the ground up, but every now and again
I'm getting used to hibernate but every now and again I hit a stumble,
I use the following JQuery in chrome: $(this).val().length this represents an input field. Now
Every now and then (ahem...) my code crashes on some system; quite often, my
Every now and then I receive a Word Document that I have to display
Every now and then I need to call new[] for built-in types (usually char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.