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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:07:10+00:00 2026-06-05T13:07:10+00:00

I’m having trouble generating random numbers that do not follow a discrete uniform distribution.

  • 0

I’m having trouble generating random numbers that do not follow a discrete uniform distribution.

So for example, say I have 5 numbers (to keep it simple), a probability of number k being generated would be k/15. (k = 1 to 5)

My idea is to generate a random number j using rand(), and if this number j is:

1 => then number 1 is generated

2 or 3 => num 2

4 or 5 or 6 => num 3

7 or 8 or 9 or 10 => num 4

11 or 12 or 13 or 14 or 15 => num 5

Now scale this to generating 1-10, 1-100, 1-1000. Does this work the way I intend it to? I’ve constructed a loop that pretty much does this every time a number needs to be generated, I think it’s probably inefficient since it goes up until it finds the j number generated in one of the ranges… What could be a better way to do this?

EDIT: or maybe create an array once with the proper numbers and then pull out with rand() better solution?

  • 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-05T13:07:12+00:00Added an answer on June 5, 2026 at 1:07 pm

    Consider that the sum s of integers from 1 to n is s = n * (n + 1) / 2. Solve for n to get n = (± sqrt(8*s + 1) - 1) / 2. We can ignore the negative square root, as we know n is positive. Thus n = (sqrt(8*s + 1) - 1) / 2.

    So, plugging in integers for s between 1 and 15:

    s  n
    01 1.000000
    02 1.561553
    03 2.000000
    04 2.372281
    05 2.701562
    06 3.000000
    07 3.274917
    08 3.531129
    09 3.772002
    10 4.000000
    11 4.216991
    12 4.424429
    13 4.623475
    14 4.815073
    15 5.000000
    

    If we take the ceiling of each computed n (the smallest integer not less than n), we get this:

    s  n
    01 1
    02 2
    03 2
    04 3
    05 3
    06 3
    07 4
    08 4
    09 4
    10 4
    11 5
    12 5
    13 5
    14 5
    15 5
    

    Thus you can go from the uniform distribution to your distribution in constant space and constant time (no iteration and no precomputed tables):

    double my_distribution_from_uniform_distribution(double s) {
        return ceil((sqrt(8*s + 1) - 1) / 2);
    }
    

    N.B. This relies on sqrt giving an exact result for a perfect square (e.g. returning exactly 7 given exactly 49). This is normally a safe assumption, because IEEE 754 requires exact rounding of square roots.

    IEEE 754 doubles can represent all integers from 1 through 2^53 (and many larger integers, but not contiguously after 2^53). So this function should work correctly for all s from 1 to floor((2^53 - 1) / 8) = 1125899906842623.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.