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 6628665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:09:05+00:00 2026-05-25T22:09:05+00:00

i am not quite sure how this function in c++ works: int rand_0toN1(int n)

  • 0

i am not quite sure how this function in c++ works:

int rand_0toN1(int n) {
    return rand() % n;
}

Another tutorial on internet says to get a random number between a range you need to do something different however, with a being first number in range and n is number of terms in range:

int number = a + rand( ) % n;

I have read that it is supposed to return a random number between the value of 0 and n-1, but how does it do that? I understand that % means divide and give the remainder (so 5 % 2 would be 1) but how does that end up giving a number between 0 and n-1? Thanks for help in understanding this. I guess i don’t understand what the rand() function returns.

  • 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-25T22:09:05+00:00Added an answer on May 25, 2026 at 10:09 pm

    The modulo (remainder) of division by n > 0 is always in the range [0, n); that’s a basic property of modular arithmetic.

    a + rand() % n does not return a number in the range [0, n) unless a=0; it returns an int in the range [a, n + a).

    Note that this trick does not in general return uniformly distributed integers.

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

Sidebar

Related Questions

I am trying to get this lightbox to work and I'm not quite sure
I'm very new to threading and thus am not quite sure if this even
Not quite sure how to word this question. I am wondering if there is
I'm not quite sure how to go about styling this particular bit of php:
I'm not quite sure how to approach this issue: I am creating a web
I'm not quite sure how to do this. Let's say I have 2 associative
I'm not quite sure the best way to deal with this situation. Here's a
I got this error message and I'm not quite sure whats wrong: Exception in
Ok, I'm not quite sure how to go about this. Here's the scenario. 1)
Right this is confusing me quite a bit, i'm not sure if any of

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.