Documentation
¶
Overview ¶
Package uniform implements a uniform continuous random variable
Uniform distribution is a probability distribution that has constant probability.
The distribution is parameterized by two parameters, namely, a and b, which are its minimum and maximum values, respectively.
References:
https://en.wikipedia.org/wiki/Uniform_distribution_(continuous) http://mathworld.wolfram.com/UniformDistribution.html
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Uniform ¶
type Uniform struct {
// contains filtered or unexported fields
}
Uniform is a continuous random variable for a uniform probability distribution parameterized by minimum and maximum values, namely, a and b, respectively.
Example ¶
Example - Uniform Distribution
package main
import (
"fmt"
"strings"
"github.com/shivakar/random/distribution/uniform"
"github.com/shivakar/random/prng/xorshift128plus"
)
func main() {
// Example based on
// http://www.cplusplus.com/reference/random/normal_distribution/
r := xorshift128plus.New(20170611)
d := uniform.New(r, 3.0, 7.0)
var p [10]int
nrolls := 10000
nstars := 100
for i := 0; i < nrolls; i++ {
n := d.Float64()
if n >= 0.0 && n < 10.0 {
p[int(n)]++
}
}
fmt.Println("Uniform Distribution: a=3.0, b=7.0")
for i := 0; i < 10; i++ {
v := p[i] * nstars / nrolls
fmt.Printf("%2d-%2d: %s (%d)\n", i, i+1,
strings.Repeat("*", v), v)
}
}
Output: Uniform Distribution: a=3.0, b=7.0 0- 1: (0) 1- 2: (0) 2- 3: (0) 3- 4: ************************ (24) 4- 5: ************************* (25) 5- 6: ************************* (25) 6- 7: ************************ (24) 7- 8: (0) 8- 9: (0) 9-10: (0)
func (*Uniform) CDF ¶
CDF or cumulative distribution function returns the probability that a real-valued random variable X of the probability distribution will be found to have a value less than or equal to x
For uniform distribution:
= 0 for x < a
CDF(x) = (x-a)/(b-a) for a <= x <= b
= 1 for x > b
func (*Uniform) Float64 ¶
Float64 returns the next random number satisfying the underlying probability distribution
For uniform distribution, since the underlying PRNG Engine already produces continuous uniform numbers between [0, 1), a simple linear transformation is sufficient to generate random variates
n = a + (b-a)*U