gpt4 book ai didi

algorithm - 如何生成 float 的随机分区并且每个部分都有最小值pMin?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:22:56 26 4
gpt4 key购买 nike

例如:

我想把33.38分成5份,每份都等于或大于1.2:

2.71,3.3,18.7,1.56,7.11

伪代码中的函数:

public static void printRandomPartition(float value,int numberOfParts,float pMin){
}

public static void main(String[] args){
//it will print 2.71,3.3,18.7,1.56,7.11
printRandomPartition(33.38,5,1.2);
}

另一种情况:

将18.77分成3份,每份等于或大于3.2:

4.8,9.55,4.42

当然输入和输出可能是丑陋的格式 float ,例如 3.82000000000003,这只是示例而已。

我查了一些帖子,好像大部分都是关于整数的,而且整数中使用的方法经常使用array int myArray[n](n是要除的整数),这对处理 float 没有用。谁能帮忙?

最佳答案

伪代码

function Divide(Sum, NParts, MinValue)
//subtract minimal values to deal with corrected sum
ASum = Sum - MinValue * NParts

//generate uniform randoms (in any reasonable range), take their sum
BSum = 0
for i = 0.. NParts - 1
Part[i] = Random
BSum = BSum + Part[i]

//scale randoms to get right corrected sum, then add minimal value to get initial sum
for i = 0.. NParts - 1
Part[i] = Part[i] * ASum / BSum + MinValue

关于algorithm - 如何生成 float 的随机分区并且每个部分都有最小值pMin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30660057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com