当前位置: 代码迷 >> 综合 >> 原始数据反推上下浮动随机数分析
  详细解决方案

原始数据反推上下浮动随机数分析

热度:34   发布时间:2024-02-11 02:36:14.0

 

需求:
1、原始数据为A

2、B为数组包含3个元素,3个元素的平均值为A

3、C为3个数组组成,每个数组由3个元素的平均值为B中的对应元素值

4、给定的原始数据A范围为0-6,不可为负数不能大于6,A变化区间 +- 0.5

 

import random# 设置数组:上线浮动区间值
def setFloatList(first,accuracy):first = float(first)accuracy = float(accuracy)if first == 0:return [0,0,0]if 0 < first <= 6:one = first - accuracytwo = firstthree = first + accuracyif one < 0: one = firstif three > 6: three = firstsecond = [float(one),float(two),float(three)]second = list(set(second))second.sort()return secondelse:return ["--","--","--"]# 获取随机列表乱序
def getRandomList(testList):test = []randomOneInt = testList[random.randint(0, 2)]test.append(randomOneInt)if testList.index(randomOneInt) == 0:randomTwoInt = testList[random.randint(1, 2)]if testList.index(randomOneInt) == 2:randomTwoInt = testList[random.randint(0, 1)]if testList.index(randomOneInt) == 1:randomTwoInt = testList[random.randint(0, 2)]test.append(randomTwoInt)equation = "(%s+%s+x)/3=%s"%(test[0],test[1],testList[1])test.append(s(equation))return test# 解方程公式  (1+1+x)/3=1,求x的值为
def s(eq, var="x"):r = eval(eq.replace("=", "-(") + ")", {var:1j})return round(-r.real / r.imag, 2)def getSecond(first):second = setFloatList(first,0.5)if len(second) == 2:if second[0] < 1:return [second[0],second[0],second[0]]else:return [second[1],second[1],second[1]]if len(second) == 3:if second[0] == second[2]:return secondelse:return getRandomList(second)def getThird(first):third = setFloatList(first,0.25)if len(third) == 2:if third[0] < 1:return [third[0],third[0],third[0]]else:return [third[1],third[1],third[1]]if len(third) == 3:if third[0] == third[2]:return thirdelse:return getRandomList(third)def getLineData(first):second = getSecond(first)LineData = []for line in range(len(second)):LineData.append( getThird(second[line]) + [second[line]] )return LineDataif __name__ == '__main__':first = 2.5print(first)# print(getSecond(first, 0.5))# print(getThird(0.5, 0.25))print(getLineData(0))

 

 

 

  相关解决方案