xsl-list
[Top] [All Lists]

Re: [xsl] Random number generator that returns numbers from a normal probability distribution and with a specified standard deviation?

2020-06-02 08:48:17
I have an XQuery function that does it. It depends on an underlying function (here rand:uniform) to give uniform numbers in [0,1].  Most "random" functions will give you uniformly distributed random numbers; if your platform doesn't have one, you'll have to concoct your own pseudo-random function

This is the polar variant of the Box-Muller algorithm

(:
 : normal()
 : Return random normally distributed data.
 :
 : $mean: Mean of range of values
 : $std: Standard deviation of range of values
 :)
declare function rand:normal(
  $mean as xs:double,
  $std as xs:double
) as xs:double
{
  $mean + rand:gauss() * $std
};

(:
 : gauss()
 : Return random normally distributed data between 0 and 1
 : A service function used by normal()
 :)
declare %private function rand:gauss() as xs:double
{
  let $u as xs:double := 2 * rand:uniform(0,1) - 1
  let $v as xs:double := 2 * rand:uniform(0,1) - 1
  let $r := $u * $u + $v * $v
  return (
    if ($r = 0 or $r >= 1) then rand:gauss()
    else (
      let $c as xs:double := math:sqrt(-2 * math:log($r) div $r)
      return $u * $c
    )
  )
};


On 5/31/20 10:37 AM, Roger L Costello costello(_at_)mitre(_dot_)org wrote:
Hi Folks,

I need a random number generator that returns numbers from a normal probability 
distribution, centered around zero, and with standard deviation that can be 
specified. Has anyone created such a thing?

I am using XSLT/XPath 2.0

/Roger

--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>