Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在地图中循环的Golang不会返回不一致的值_Go - Fatal编程技术网

在地图中循环的Golang不会返回不一致的值

在地图中循环的Golang不会返回不一致的值,go,Go,我正在写一个概率模型,它可以接受任意数量的“结果”,然后当一个数字被滚动并传递到模型中时,正确的“结果”将被返回 从本质上讲,逻辑是一张结果图,其中的索引表示该结果的特定权重 结果一25% 结果二25% 结果三50% 这些价值观将转化为: outcomes := make(map[int]Outcome) outcomes[25] = Outcome{"Outcome One", 25} outcomes[50] = Outcome{"Outcome One", 25} outcomes[100

我正在写一个概率模型,它可以接受任意数量的“结果”,然后当一个数字被滚动并传递到模型中时,正确的“结果”将被返回

从本质上讲,逻辑是一张结果图,其中的索引表示该结果的特定权重

结果一25% 结果二25% 结果三50%

这些价值观将转化为:

outcomes := make(map[int]Outcome)
outcomes[25] = Outcome{"Outcome One", 25}
outcomes[50] = Outcome{"Outcome One", 25}
outcomes[100] = Outcome{"Outcome One", 50}
我有一个函数,然后接受一个输入,比如说10,并围绕结果循环,直到指数大于输入

期望

input: 10, output: Outcome{"Outcome One", 25}
input: 30, output: Outcome{"Outcome Two", 25}
input: 60, output: Outcome{"Outcome Two", 50}
然而,在输入10的单元测试中,我得到了“结果1”和“结果2”的组合,我认为问题在于for循环

ProbabilityMatrix_test.go

var outcome1 = Outcome{"Outcome One", 25}
var outcome2 = Outcome{"Outcome Two", 25}
var outcome3 = Outcome{"Outcome Three", 50}

probabilityMatrix := ProbabilityMatrix{}
probabilityMatrix.SetUp()

probabilityMatrix.AddOutcome(outcome1)
probabilityMatrix.AddOutcome(outcome2)
probabilityMatrix.AddOutcome(outcome3)

outcome := probabilityMatrix.RollA(10)

if outcome != outcome1 {
    t.Errorf("%s", probabilityMatrix.Delimiters)
    t.Errorf("incorrect outcome, got %s, expected %s", outcome.Name, outcome1.Name)
}
下面的代码返回结果1,大约75%的时间(正确),结果2返回25%

package RealisticTemperatureGenerator

type Outcome struct {
    Name        string
    Probability int
}

type ProbabilityMatrix struct {
    Delimiters     map[int]Outcome
    DefaultOutcome Outcome
    Total          int
}

func (pm *ProbabilityMatrix) SetUp() {
    pm.Delimiters = make(map[int]Outcome)
    pm.Total = 0
}

func (pm *ProbabilityMatrix) AddOutcome(outcome Outcome) {

    pm.DefaultOutcome = outcome
    currentIndex := outcome.Probability + pm.Total
    pm.Delimiters[currentIndex] = outcome
    pm.Total = currentIndex
}

func (pm *ProbabilityMatrix) RollA(input int) Outcome {
  return pm.WalkDelimiters(input)
}

// Problem Possibly here
func (pm ProbabilityMatrix) WalkDelimiters(input int) Outcome {

  for key, _ := range pm.Delimiters {
      if pm.Delimiters[key].Probability >= input {
        return pm.Delimiters[key]
      }
  }
  return pm.DefaultOutcome
}

在golang中循环映射时,返回元素的顺序是随机的。这就是行为不一致的原因。参见官方博客中的“迭代顺序”:

如果您想要稳定的订单,您必须在另一个结构中维护密钥:

keys := []int{25, 50, 100}

for _, key := range keys {
    if pm.Delimiters[key].Probability >= input {
        return pm.Delimiters[key]
    }
}

这是惊人的,非常感谢,我将在9分钟内标记为excepted:)另请参见相关内容:如果您正在集中查找&希望在不迭代的情况下快速得到结果,那么您应该为映射中的每个百分比值添加一个键值对。这应该注意了,尤其是你的输入总是整数。