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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:29:13+00:00 2026-05-13T21:29:13+00:00

I like to use MySQL to do quantitative analysis and statistics. I would like

  • 0

I like to use MySQL to do quantitative analysis and statistics.
I would like to make a MySQL user-defined function of the form:
sample_gaussian(mean, stdev) that returns a single randomized
value sampled from a gaussian distribution having mean and standard
deviation of the user-entered arguments. MySQL already has a
function rand() that returns a random number, so I just need to
know some pseudocode for constraining/transforming that value
so that it falls into the right distribution.
Any suggestions?

BTW- This is my first stackoverflow question, so please forgive
me if this question is asking too much of users on this site.

  • 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-13T21:29:13+00:00Added an answer on May 13, 2026 at 9:29 pm

    In answer to my own question, here is a MySQL user-defined function that returns a single random value sampled from a Gaussian distribution with a given mean and standard deviation.

    DROP FUNCTION IF EXISTS gauss;
    DELIMITER //
    CREATE FUNCTION gauss(mean float, stdev float) RETURNS float
    BEGIN
    set @x=rand(), @y=rand();
    set @gaus = ((sqrt(-2*log(@x))*cos(2*pi()*@y))*stdev)+mean;
    return @gaus;
    END
    //
    DELIMITER ;
    

    To verify that this is in fact returning a Gaussian distribution, you can generate a series of these, then plot a histogram:

    create temporary table temp (id int, rando float);
    insert into temp (rando) select gauss(2,1); # repeat this operation 500 times
    insert into temp (rando) select gauss(2,1) from any_table_with_500+_entries limit 500;
    select round(temp,1), count(*) from temp group by round(temp,1) # creates a histogram
    

    If you plot that histogram in excel or graphing tool of choice, you’ll see the bell shaped normal curve.

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

Sidebar

Related Questions

I'd like to use a single SQL query (in MySQL) to find the record
I would like to use a language that I am familiar with - Java,
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
It seems like most people use MySQL together with PHP. I've used H2 and
Is it good to use mysql_close in PHP5? Can I do like Open mysql
I like to use static functions in C++ as a way to categorize them,
I like to use Emacs' shell mode, but it has a few deficiencies. One
I like to use IPython's zope profile to inspect my Plone instance, but a
Most websites use - (like Stack Overflow) but most PHP frameworks generate + encoded

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.