问题描述:产生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已知平均数求随机数的更多相关文章

  1. 已知 $AB$, 求 $BA$

    设 $A,B$ 分别是 $3\times 2$ 和 $2\times 3$ 实矩阵. 若 $\dps{AB=\sex{\ba{ccc}  8&0&-4\\  -\frac{3}{2}& ...

  2. C语言初学 给已知公式求圆周率

    公式: 圆周率=1-1/3+1/5-1/7+......+1/(4n-3)-1/(4n-1) #include<stdio.h> #include<math.h> main() ...

  3. Codeforce 459A - Pashmak and Garden (已知两点求另外两点构成正方形)

    Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashm ...

  4. python应用-已知三角形的边长求他的面积和周长

    """ 已知三角形的边长求他的面积和周长 Author:罗万财 Date:2017-3-3 """ import math a=float( ...

  5. JAVA-集合作业-已知有十六支男子足球队参加2008 北京奥运会。写一个程序,把这16 支球队随机分为4 个组。采用List集合和随机数

    第二题 已知有十六支男子足球队参加2008 北京奥运会.写一个程序,把这16 支球队随机分为4 个组.采用List集合和随机数 2008 北京奥运会男足参赛国家: 科特迪瓦,阿根廷,澳大利亚,塞尔维亚 ...

  6. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  7. [YY]已知逆序列求原序列(二分,树状数组)

    在看组合数学,看到逆序列这个概念.于是YY了一道题:已知逆序列,求出原序列. 例子: 元素个数 n = 8 逆序列 a={5,3,4,0,2,1,1,0} 则有原序列 p={4,8,6,2,5,1,3 ...

  8. 已知ip地址和其子网掩码如何求网络号子网号主机号

    已知ip地址为10.130.89.95,其子网掩码为255.255.255.224,求其网络号.子网号和主机号. 要看子网掩码变长在第几节,255.255.255.224是在第四节借了位 把224转换 ...

  9. 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数。

    描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数.   输入 第一行为M,表示测试数据组数.接下来M行,每行包含一个测试数据. 输出 ...

随机推荐

  1. zabbix实现mysql数据库的监控(一)

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.它能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问 ...

  2. CommonJS与AMD、CMD

    随着JS模块化编程的发展,处理模块之间的依赖关系变得至关重要,随后诞生了CommonJS.AMD与CMD规范,但es6的import/export能代替他们,但因为本人所使用的webpack也支持前三 ...

  3. hihocoder 1142 三分求极值【三分算法 模板应用】

    #1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一 ...

  4. EntityFramework 学习 一 Table-Valued Function in Entity Framework 5.0

    USE [SchoolDB] GO /****** Object: UserDefinedFunction [dbo].[GetCourseListByStudentID] */ SET ANSI_N ...

  5. LINQ 学习路程 -- 查询操作 Skip SkipWhile Take TakeWhile

    Method Description Skip 跳过序列中指定数量元素,然后返回剩余序列 SkipWhile 只要满足条件,就跳过序列中的元素,然后返回剩余函数 Take 从序列的开头返回指定数量的连 ...

  6. 查询速度优化用not EXISTS 代替 not in

    1,not in 速度奇慢,要用 not EXISTS ,速度奇快! 大表效果尤其明显 sql中exists,not exists的用法 exists()后面的子查询被称做相关子查询,他是不返回列表的 ...

  7. java:stack栈: Stack 类表示后进先出(LIFO)的对象堆栈

    //Stack 类表示后进先出(LIFO)的对象堆栈 //它提供了通常的 push 和 pop 操作,以及取栈顶点的 peek 方法.测试堆栈是否为空的 empty 方法.在堆栈中查找项并确定到栈顶距 ...

  8. MVC中Controller弹出提示框

    1.删除时查询是否有先关数据,有提示删除相关数据:成功刷新页面:失败提示删除失败 Controller: 有相关数据:return RedirectToAction("Index" ...

  9. Python3之hashlib模块

    Python3之hashlib   简介: 用于加密相关的操作,代替了md5模块和sha模块,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法.在python3中已 ...

  10. Convolutional Neural Networks for Visual Recognition 7

    Two Simple Examples softmax classifier 后,我们介绍两个简单的例子,一个是线性分类器,一个是神经网络.由于网上的讲义给出的都是代码,我们这里用公式来进行推导.首先 ...