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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:07:32+00:00 2026-06-03T05:07:32+00:00

Needing a random number generator that returns a sample from a normal (Gaussian) distribution,

  • 0

Needing a random number generator that returns a sample from a normal (Gaussian) distribution, I’ve ported to F# a portion of John D. Cook’s C# generator:

let mutable m_w = 521288629u
let mutable m_z = 362436069u

let private getUint () =
    m_z <- 36969u * (m_z &&& 65535u) + (m_z >>> 16)
    m_w <- 18000u * (m_w &&& 65535u) + (m_w >>> 16)
    (m_z <<< 16) + m_w

let private setSeed () =
    let dt = System.DateTime.Now
    let x = dt.ToFileTime ()
    m_w <- uint32 (x >>> 16)
    m_z <- uint32 (x % 4294967296L)

let private getUniform () =
    let u = getUint ()
    (float u + 1.) * 2.328306435454494e-10

let private randomNormal () =
    let u1 = getUniform ()
    let u2 = getUniform ()
    let r = sqrt (-2. * (log u1))
    let theta = 2. * System.Math.PI * u2
    r * sin (theta)

/// Returns a normal (Gaussian) random sample with mean 0 and standard deviation 1
let randn () =
    setSeed ()
    randomNormal ()

/// Returns an array of normal (Gaussian) random samples
let randns n m =
    setSeed ()
    [| for i in 0 .. n - 1 -> randomNormal () |]

This implementation works fine but is not thread safe. Given that the code that depends on it makes extensive use of the Thread Parallel Library, I need to make it thread safe.

This does not look obvious to me because at the core of the method lie two mutable members which are pretty much indispensable. Is there any other way to achieve thread-safety without resorting to locks?

Is there any other way to implement a normal pseudo-random generator using only immutable members?

  • 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-03T05:07:34+00:00Added an answer on June 3, 2026 at 5:07 am

    Using mutable members, you’d have no choice but use a lock.

    However, you’d be better off with an immutable record containing m_w and m_z that you pass to your random functions. They could return a tuple of your random value and a new record containing the updated random members. Better yet, you could create a computation expression to handle generating randoms so you won’t have to worry about passing the random record around.

    Also, calling setSeed from within your random functions is bad. Multiple subsequent calls will return the same vale. You only want to set your seed once.

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

Sidebar

Related Questions

I often find myself needing reference to an object that is several objects away,
I'm needing to create a zip file containing documents that exist on the server.
I'm needing an excel formula to output the mineral name from the example list
I find myself needing to call back into the JVM from arbitrary native threads
So I found myself needing to remove <br /> tags from the beginning and
I'm faced with needing access for reporting to some data that lives in Oracle
I'm needing to pull 3 rows from a table for a featured section on
I am currently needing to validate that a checkbox list is a required field
I am needing code that will run in visual basic to capture the screen
I am needing to write a script that will move files to a destination

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.