Jump to content

Random numbers: Difference between revisions

(Realize in F#)
(→‎{{header|Go}}: add output)
Line 866:
 
import (
"fmt"
"math"
"math/rand"
"strings"
"time"
)
Line 872 ⟶ 875:
const mean = 1.0
const stdv = .5
const n = 1000
 
func main() {
var list [1000n]float64
rand.Seed(time.Now().UnixNano())
for i := range list {
list[i] = mean + stdv*rand.NormFloat64()
}
// show computed mean and stdv of list
var s, sq float64
for _, v := range list {
s += v
}
cm := s / n
for _, v := range list {
d := v - cm
sq += d * d
}
fmt.Printf("mean %.3f, stdv %.3f\n", cm, math.Sqrt(sq/(n-1)))
// show histogram by hdiv divisions per stdv over +/-hrange stdv
const hdiv = 3
const hrange = 2
var h [1 + 2*hrange*hdiv]int
for _, v := range list {
bin := hrange*hdiv + int(math.Floor((v-mean)/stdv*hdiv+.5))
if bin >= 0 && bin < len(h) {
h[bin]++
}
}
const hscale = 10
for _, c := range h {
fmt.Println(strings.Repeat("*", (c+hscale/2)/hscale))
}
}</lang>
{{out}}
<pre>
mean 0.995, stdv 0.503
**
****
******
********
************
************
*************
************
**********
********
*****
***
**
</pre>
 
=={{header|FutureBasic}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.