dota220141003 2016-05-12 19:58
浏览 63
已采纳

随机指数分布怪异度

I stumbled onto this, 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.

  • 写回答

2条回答 默认 最新

  • dpzbzp8728 2016-05-17 21:40
    关注

    First, your X-scale is misleading, as it starts from 1 and ends with 10. Should be 0...9.

    Considering that it would be fixed, your distribution is fully correct, though maybe not intended (what did you actually want?).

    You first have a distribution between 0 and 9, both inclusive. If you add 0.5 and then round down, ask yourself how many hits each index can acually "get"?

    A: Most indexes get a "full set" with decimal values between 1 and 2 (or 6 and 7, or any other interval) which gets rounded down to 1 (or 6, or any index)

    EXCEPT

    The edge indexes 0 and 9 only get a "half full set".

    Because you offset index 0...1 to 0.5...1.5 and round down. Only half of this range will then remain for index=0, ie. values between 0.5 and 1 (as there are no longer any between 0 and 0.5).

    Same with other end. You offset 8...9 to 8.5...9.5 and then round down. Index 9 only gets 1/2, ie. values between 9 and 9.5.

    The left end of your chart is actually lower than you probably expected, though it's not as distinguishable as the right end.

    The numbers are indeed sometimes surprising :-).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题