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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog