jeudi 12 mai 2016

Random index distribution weirdness

I stumbled onto this looking, trying to do a random biased sample from some data. It seems a simple distribution fitted to x^2 is what I'm looking for, but there's an artefact here I can't quite wrap my head around.

Here's a snippet of a for loop selecting an index in an array distributed by x^2, and then incrementing the counter at that index position.

package main
import "time"
import "fmt"
import "math"
import "math/rand"

func main() {
  rand.Seed(time.Now().UTC().UnixNano())

  var arr [10]int

  for i := 0; i < 5000; i++ {
    rnd := rand.Float64()
    tmp := rnd * rnd * 9

    index := int(math.Floor(tmp + .5))

    arr[index]++
  }
  fmt.Printf("%v", arr)
}

No matter the bounds or the number of iterations, plotting the values the graph always comes out looking like this, with a noticable "drop" at the end.

enter image description here

This is what I have trouble understanding. Shouldn't the indexes fit the curve all the way?

I'm suspecting something related to the rounding, but I'm grasping for straws at the moment.




Aucun commentaire:

Enregistrer un commentaire