使用python已知平均数求随机数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python已知平均数求随机数相关的知识,希望对你有一定的参考价值。

问题描述:产生40个数,范围是363-429之间,平均值为402

思路:

1 产生一个随机数

2 使用平均数求和随机数求出第二个数,生成20组

3 将排序打乱

# -*- coding: cp936 -*-
import random
import string

###################产生随机整数###################
###################第一个数随机产生,第二个使用平均数求出###################
#count 数字的个数
#average 平均数
#begin 起始区间
#end 结束区间
def int_random (count, average, begin, end):

    #print "wzh_random"
    numarr = [0 for x in range(2)];
    i = 0;
    while (1):
 
      num_first = random.randrange(begin, end);
 
      #第二个数
      num_second = average * 2 - num_first;

      if (num_second >= begin and num_second <= end):
          numarr[i] = num_first;
          i = i + 1;
          numarr[i] = num_second;
          break

    return numarr;

###################产生随机数###################
###################第一个数随机产生,第二个使用平均数求出###################
#count 数字的个数
#average 平均数
#begin 起始区间
#end 结束区间
def float_random (count, average, begin, end):

    #print "wzh_random"
    numarr = [0 for x in range(2)];
    i = 0;
    while (1):
 
      num = random.uniform(begin, end);
      #取两位小数
      num_first = round(num, 2);

      #第二个数
      num_second = average * 2 - num_first;

      if (num_second >= begin and num_second <= end):
          numarr[i] = num_first;
          i = i + 1;
          numarr[i] = num_second;
          break

    return numarr;



###################写文件###################
def write_file (filename, content):
    fo = open (filename, "ab");
    fo.write(content);
    fo.close();

def show_list (list):
    for i in list:
        print i,
    print;

###################主函数调用产生整形随机数###################
#40个数字,平均数400,363 - 429 之间
def test_random_int():
    count = 40;
    average = 402;
    begin = 363;
    end = 429;
    numarr_count = 0;
    numarr = [0 for x in range(count)];
    for i in range (count / 2):
        list = int_random (40, 402, 363, 429)
        j = 0;
        for j in range (len(list)):
             numarr[numarr_count] = list[j];
             numarr_count += 1;
    content = ‘‘;
    #打乱排序
    print "数据未打乱:";
    show_list (numarr)
    random.shuffle(numarr);
    print "数据打乱:";
    show_list (numarr)
    for i in numarr:
        content = content +   + str(i);
    #print content;
    #追加写入文件
    filename = "test.txt";
    print "文件名称:",filename;
    write_file (filename, content)
    write_file (filename, "\n");

###################主函数调用产生实型随机数###################
#40个数字,平均数400,363 - 429 之间
def test_random_float():
    count = 40;
    average = 402;
    begin = 363;
    end = 429;
    numarr_count = 0;
    numarr = [0 for x in range(count)];
    for i in range (count / 2):
        list = float_random (40, 402, 363, 429)
        j = 0;
        for j in range (len(list)):
             numarr[numarr_count] = list[j];
             numarr_count += 1;
    content = ‘‘;
    #打乱排序
    print "数据未打乱:";
    show_list (numarr)
    random.shuffle(numarr);
    print "数据打乱:";
    show_list (numarr)
    for i in numarr:
        content = content +   + str(i);
    #print content;
    #追加写入文件
    filename = "test.txt";
    print "文件名称:",filename;
    write_file (filename, content)
    write_file (filename, "\n");

#调用测试产生整形随机数
test_random_int();
#调用测试产生实型随机数
test_random_float();

 

以上是关于使用python已知平均数求随机数的主要内容,如果未能解决你的问题,请参考以下文章

python求最大值、最小值、求和、平均值

python求一组数组最大值,最小值,平均值

Python实现txt文件读取数字,排序,求均值?

python已知cos求弧度

JAVA怎样随机生成10W个数字, 要求: 10W个数字总等于50W而且每个数字最小1最大100, 求代码及思路

关于python计算的问题。怎样计算平均值,最大值和最小值???