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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:33:52+00:00 2026-05-22T20:33:52+00:00

What is some code to generate normally distributed random numbers in ruby? (Note: I

  • 0

What is some code to generate normally distributed random numbers in ruby?

(Note: I answered my own question, but I’ll wait a few days before accepting to see if anyone has a better answer.)

EDIT:

Searching for this, I looked at all pages on SO resulting from the two searches:

+”normal distribution” ruby

and

+gaussian +random ruby

  • 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-22T20:33:53+00:00Added an answer on May 22, 2026 at 8:33 pm

    Python’s random.gauss() and Boost’s normal_distribution both use the Box-Muller transform, so that should be good enough for Ruby too.

    def gaussian(mean, stddev, rand)
      theta = 2 * Math::PI * rand.call
      rho = Math.sqrt(-2 * Math.log(1 - rand.call))
      scale = stddev * rho
      x = mean + scale * Math.cos(theta)
      y = mean + scale * Math.sin(theta)
      return x, y
    end
    

    The method can be wrapped up in a class that returns the samples one by one.

    class RandomGaussian
      def initialize(mean, stddev, rand_helper = lambda { Kernel.rand })
        @rand_helper = rand_helper
        @mean = mean
        @stddev = stddev
        @valid = false
        @next = 0
      end
    
      def rand
        if @valid then
          @valid = false
          return @next
        else
          @valid = true
          x, y = self.class.gaussian(@mean, @stddev, @rand_helper)
          @next = y
          return x
        end
      end
    
      private
      def self.gaussian(mean, stddev, rand)
        theta = 2 * Math::PI * rand.call
        rho = Math.sqrt(-2 * Math.log(1 - rand.call))
        scale = stddev * rho
        x = mean + scale * Math.cos(theta)
        y = mean + scale * Math.sin(theta)
        return x, y
      end
    end
    

    CC0 (CC0)

    To the extent possible under law, antonakos has waived all copyright and related or neighboring rights to the RandomGaussian Ruby class. This work is published from: Denmark.

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

Sidebar

Related Questions

I've written some code to generate a sequence of random characters, but it does
I am having a php file which executes some code to generate a html
I generate some code using CXF from a WSDL-file. When compiling the code with
I'm trying to generate some code at runtime where I put in some boiler-plate
Using T4 I want to generate some code based on examining what files are
I am trying to generate some code at runtime using the DynamicMethod class in
I am trying to generate some code using CodeDom. I wanted to know if
I have a task and I want to generate some code using the CodeDom.
I am writing a macro for Visual studio that will generate some code. I
I have some very simple code to generate an assembly and invoke a method

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.