带有尖峰的随机数gen

时间:2018-08-01 05:56:00

标签: python-3.x

我正在像这样在python中生成一些数据:

import random
import time

def generate_random_data():
    pressure = float(random.randint(2409,2415) / 100)
    temp = float(random.randint(6989,7045) / 100)
    data = {"pressure": pressure, "temperature": temp}
    return data

if __name__ == '__main__':
    while True:
        rslt = generate_random_data()
        print(rslt)
        time.sleep(60)

但是我还想做的是每隔一段时间生成一次超出范围的尖峰...在生成范围内的数字时我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

您的代码有一些更新:

import random
import time

def generate_random_data():
    peak_chance = random.randint(0, 100)
    is_peak = False

    if peak_chance < 10:  # 10% chance for under-value
        pressure = float(random.randint(2309, 2409) / 100)
        temp = float(random.randint(6889, 6989) / 100)
        is_peak = True
    elif peak_chance > 90:  # 10% chance for peak-value
        pressure = float(random.randint(2415, 2515) / 100)
        temp = float(random.randint(7045, 7145) / 100)
        is_peak = True
    else:
        pressure = float(random.randint(2409, 2415) / 100)
        temp = float(random.randint(6989, 7045) / 100)

    data = {"pressure": pressure, "temperature": temp, "peak": is_peak}
    return data


if __name__ == '__main__':
    while True:
        rslt = generate_random_data()
        print(rslt)
        time.sleep(1)

输出:

{'pressure': 24.14, 'temperature': 70.4, 'peak': False}
{'pressure': 24.13, 'temperature': 70.2, 'peak': False}
{'pressure': 24.11, 'temperature': 69.97, 'peak': False}
{'pressure': 24.14, 'temperature': 70.42, 'peak': False}
{'pressure': 24.12, 'temperature': 69.95, 'peak': False}
{'pressure': 24.1, 'temperature': 70.26, 'peak': False}
{'pressure': 24.09, 'temperature': 69.95, 'peak': False}
{'pressure': 24.12, 'temperature': 70.02, 'peak': False}
{'pressure': 24.12, 'temperature': 70.0, 'peak': False}
{'pressure': 24.14, 'temperature': 70.15, 'peak': False}
{'pressure': 24.1, 'temperature': 70.3, 'peak': False}
{'pressure': 23.57, 'temperature': 69.8, 'peak': True}
{'pressure': 24.15, 'temperature': 70.13, 'peak': False}
{'pressure': 24.14, 'temperature': 70.23, 'peak': False}
{'pressure': 24.74, 'temperature': 70.7, 'peak': True}
{'pressure': 24.12, 'temperature': 70.22, 'peak': False}
{'pressure': 23.81, 'temperature': 69.24, 'peak': True}
{'pressure': 24.12, 'temperature': 70.13, 'peak': False}
{'pressure': 24.09, 'temperature': 70.33, 'peak': False}
{'pressure': 24.13, 'temperature': 69.95, 'peak': False}
{'pressure': 24.12, 'temperature': 70.13, 'peak': False}
{'pressure': 24.09, 'temperature': 70.35, 'peak': False}
{'pressure': 24.3, 'temperature': 71.18, 'peak': True}
{'pressure': 24.11, 'temperature': 70.26, 'peak': False}
{'pressure': 24.11, 'temperature': 69.98, 'peak': False}
{'pressure': 24.1, 'temperature': 70.11, 'peak': False}
{'pressure': 24.13, 'temperature': 70.16, 'peak': False}
{'pressure': 24.15, 'temperature': 70.05, 'peak': False}

答案 1 :(得分:0)

如果使用> = Python3.6(docs),则可以利用random.choices方法及其参数weights

import random
import time

ranges = {
    0.9: {'pressure': (2409, 2415), 'temp': (6989,7045)},    # 90% will be in normal range
    0.08: {'pressure': (2500, 3000), 'temp': (7100, 7500)},  # 8% will be a peak
    0.02: {'pressure': (3500, 4000), 'temp': (8100, 9500)},  # 2% will be a extreme peak
}

def generate_random_data(ranges):
    while True:
        r = random.choices(list(ranges.values()), weights=ranges.keys())[0]
        pressure =  float(random.randint(*r['pressure']) / 100)
        temp =  float(random.randint(*r['temp']) / 100)
        yield pressure, temp

if __name__ == '__main__':
    n = 10000
    generated_data = [v for i, v in zip(range(n), generate_random_data(ranges))]

    print('number of temp >= 24:', len(list(i[0] for i in generated_data if i[0] >= 24)))
    print('number of temp >= 25:', len(list(i[0] for i in generated_data if i[0] >= 25)))
    print('number of temp >= 35:', len(list(i[0] for i in generated_data if i[0] >= 35)))

    print('number of pressure >= 69:', len(list(i[1] for i in generated_data if i[1] >= 69)))
    print('number of pressure >= 71:', len(list(i[1] for i in generated_data if i[1] >= 71)))
    print('number of pressure >= 81:', len(list(i[1] for i in generated_data if i[1] >= 81)))

这将打印:

number of temp >= 24: 10000
number of temp >= 25: 1033
number of temp >= 35: 207
number of pressure >= 69: 10000
number of pressure >= 71: 1033
number of pressure >= 81: 207