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

  • Home
  • SEARCH
  • 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 833229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:29:06+00:00 2026-05-15T04:29:06+00:00

Possible Duplicate: Generating Random Numbers in Objective-C How do I generate a random number

  • 0

Possible Duplicate:
Generating Random Numbers in Objective-C

How do I generate a random number which is within a range?

  • 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-15T04:29:07+00:00Added an answer on May 15, 2026 at 4:29 am

    This is actually a bit harder to get really correct than most people realize:

    int rand_lim(int limit) {
    /* return a random number between 0 and limit inclusive.
     */
    
        int divisor = RAND_MAX/(limit+1);
        int retval;
    
        do { 
            retval = rand() / divisor;
        } while (retval > limit);
    
        return retval;
    }
    

    Attempts that just use % (or, equivalently, /) to get the numbers in a range almost inevitably introduce skew (i.e., some numbers will be generated more often than others).

    As to why using % produces skewed results: unless the range you want is a divisor of RAND_MAX, skew is inevitable. If you start with small numbers, it’s pretty easy to see why. Consider taking 10 pieces of candy (that we’ll assume you can’t cut, break, etc. into smaller pieces) and trying to divide it evenly between three children. Clearly it can’t be done–if you hand out all the candy, the closest you can get is for two kids to get three pieces of candy, and one of them getting four.

    There’s only one way for all the kids to get the same number of pieces of candy: make sure you don’t hand out the last piece of candy at all.

    To relate this to the code above, let’s start by numbering the candies from 1 to 10 and the kids from 1 to 3. The initial division says since there are three kids, our divisor is three. We then pull a random candy from the bucket, look at its number and divide by three and hand it to that kid — but if the result is greater than 3 (i.e. we’ve picked out candy number 10) we just don’t hand it out at all — we discard it and pick out another candy.

    Of course, if you’re using a modern implementation of C++ (i.e., one that supports C++11 or newer), you should usually use one the distribution classes from the standard library. The code above corresponds most closely with std::uniform_int_distribution, but the standard library also includes uniform_real_distribution as well as classes for a number of non-uniform distributions (Bernoulli, Poisson, normal, maybe a couple others I don’t remember at the moment).

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

Sidebar

Ask A Question

Stats

  • Questions 420k
  • Answers 420k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer typedef std::vector<char> char_array; typedef std::list<char_array> char_array_list; Or more appropriately: typedef… May 15, 2026 at 10:34 am
  • Editorial Team
    Editorial Team added an answer flash.utils.ByteArray is mapped to Java's byte[] type. May 15, 2026 at 10:34 am
  • Editorial Team
    Editorial Team added an answer Just ignore the CGI documentation and print your own HTML… May 15, 2026 at 10:34 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.