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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:17:02+00:00 2026-05-23T19:17:02+00:00

I’m looking for a way to generate a set of integers with a specified

  • 0

I’m looking for a way to generate a set of integers with a specified mean and std. deviation.

Using the random library, it is possible to generate a set of random doubles distributed in gaussian fashion, this would look something like this:

#include <tr1/random>

std::tr1::normal_distribution<double> normal(mean, stdDev);
std::tr1::ranlux64_base_01 eng;
eng.seed(1000);
for (int i = 0; i < N; i++)
{
    gaussiannums[i] = normal(eng);
}

However, for my application, I need integers instead of doubles. So my question is, how would you generate the equivalent of the above but for integers instead of doubles? One possible path to take is to convert the doubles into integers in some fashion, but I don’t know enough about how the random library works to know whether this can be done in a fashion that really preserves the bell shape and the mean/std. deviation.

I should mention that the goal here is not so much randomness, as it is to get a set of integers of a specific size, with the correct mean and std. deviation.

Ideally I would also like to specify the minimum and maximum values that can be produced, but I have not found any way to do this even for doubles, so any suggestions on this are also welcome.

  • 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-23T19:17:03+00:00Added an answer on May 23, 2026 at 7:17 pm

    This isn’t possible.

    The gaussian distribution is continuous, the set of integers is discrete.

    The gaussian pdf has unlimited support, if you specify minimum and maximum you’ll also have a different distribution.

    What are you really trying to do? Is it only the mean and standard deviation that count? Other distributions have a well-defined mean and standard-deviation, including several discrete distributions.


    For example, you could use a binomial distribution.

    Solve the equations for mean and variance simultaneously to get p and n. Then generate samples from this distribution.

    If n doesn’t come out integer, you can use a multinomial distribution instead.


    Although wikipedia describes methods for sampling from a binomial or multinomial distribution, they aren’t particularly efficient. There’s a method for efficiently generating samples from an arbitrary discrete distribution which you can use here.

    In the comments, you clarified that you want a bell-shaped distribution with specific mean and standard deviation and bounded support. So we’ll use the Gaussian as a starting point:

    • compute a gaussian CDF across the range of integers you’re interested in
    • offset and scale it slightly to account for the missing tails (so it varies from 0 to 1)
    • store it in an array

    To sample from this distribution:

    • generate uniform reals in the range [0:1]
    • use binary search to invert the CDF

    As the truncation step will reduce the standard deviation slightly (and affect the mean also, if the minimum and maximum aren’t equidistant from the chosen mean) you may have to tweak the Gaussian parameters slightly beforehand.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

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.