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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:19:35+00:00 2026-06-18T11:19:35+00:00

I’d like to calculate a non-uniformly distributed random number in the range [0, n

  • 0

I’d like to calculate a non-uniformly distributed random number in the range [0, n - 1]. So the min possible value is zero. The maximum possible value is n-1. I’d like the min-value to occur the most often and the max to occur relatively infrequently with an approximately linear curve between (Gaussian is fine too). How can I do this in Objective-C? (possibly using C-based APIs)

A very rough sketch of my current idea is:

// min value w/ p = 0.7
// some intermediate value w/ p = 0.2
// max value w/ p = 0.1
NSUInteger r = arc4random_uniform(10);
if (r <= 6) 
    result = 0;
else if (r <= 8)
    result = (n - 1) / 2;
else
    result = n - 1;
  • 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-06-18T11:19:36+00:00Added an answer on June 18, 2026 at 11:19 am

    I think you’re on basically the right track. There are possible precision or range issues but in general if you wanted to randomly pick, say, 3, 2, 1 or 0 and you wanted the probability of picking 3 to be four times as large as the probability of picking 0 then if it were a paper exercise you might right down a grid filled with:

    3 3 3 3
    2 2 2
    1 1
    0
    

    Toss something onto it and read the number it lands on.

    The number of options there are for your desired linear scale is:

    - 1 if number of options, n, = 1
    - 1 + 2 if n = 2
    - 1 + 2 + 3 if n = 3
    - ... etc ...
    

    It’s a simple sum of an arithmetic progression. You end up with n(n+1)/2 possible outcomes. E.g. for n = 1 that’s 1 * 2 / 2 = 1. For n = 2 that’s 2 * 3 /2 = 3. For n = 3 that’s 3 * 4 / 2 = 6.

    So you would immediately write something like:

    NSUInteger random_linear(NSUInteger range)
    {
        NSUInteger numberOfOptions = (range * (range + 1)) / 2;
        NSUInteger uniformRandom = arc4random_uniform(numberOfOptions);
    
        ... something ...
    }
    

    At that point you just have to decide which bin uniformRandom falls into. The simplest way is with the most obvious loop:

    NSUInteger random_linear(NSUInteger range)
    {
        NSUInteger numberOfOptions = (range * (range + 1)) / 2;
        NSUInteger uniformRandom = arc4random_uniform(numberOfOptions);
    
        NSUInteger index = 0;
        NSUInteger optionsToDate = 0;
        while(1)
        {
            if(optionsToDate >= uniformRandom) return index;
            index++;
            optionsToDate += index;
        }
    }
    

    Given that you can work out optionsToDate without iterating, an immediately obvious faster solution is a binary search.

    An even smarter way to look at it is that uniformRandom is the sum of the boxes underneath a line from (0, 0) to (n, n). So it’s the area underneath the graph, and the graph is a simple right-angled triangle. So you can work backwards from the area formula.

    Specifically, the area underneath the graph from (0, 0) to (n, n) at position x is (x*x)/2. So you’re looking for x, where:

    (x-1)*(x-1)/2 <= uniformRandom < x*x/2
    
    => (x-1)*(x-1) <= uniformRandom*2 < x*x
    => x-1 <= sqrt(uniformRandom*2) < x
    

    In that case you want to take x-1 as the result hadn’t progressed to the next discrete column of the number grid. So you can get there with a square root operation simple integer truncation.

    So, assuming I haven’t muddled my exact inequalities along the way, and assuming all precisions fit:

    NSUInteger random_linear(NSUInteger range)
    {
        NSUInteger numberOfOptions = (range * (range + 1)) / 2;
        NSUInteger uniformRandom = arc4random_uniform(numberOfOptions);
    
        return (NSUInteger)sqrtf((float)uniformRandom * 2.0f);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display

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.