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

 

需求:
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 - accuracy
        two = first
        three = first + accuracy

        if one < 0: one = first
        if three > 6: three = first

        second = [float(one),float(two),float(three)]
        second = list(set(second))
        second.sort()
        return second
    else:
        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 second
        else:
            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 third
        else:
            return getRandomList(third)


def getLineData(first):
    second = getSecond(first)
    LineData = []
    for line in range(len(second)):
        LineData.append( getThird(second[line]) + [second[line]] )
    return LineData


if __name__ == '__main__':
    first = 2.5
    print(first)
    # print(getSecond(first, 0.5))
    # print(getThird(0.5, 0.25))
    print(getLineData(0))

 

 

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BenjaminQA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值