使用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();
时间: 2024-10-05 05:14:40

使用python已知平均数求随机数的相关文章

先序,中序,后序,已知两者求第三者

今天来总结下二叉树前序.中序.后序遍历相互求法,即如果知道两个的遍历,如何求第三种遍历方法,比较笨的方法是画出来二叉树,然后根据各种遍历不同的特性来求,也可以编程求出,下面我们分别说明. 首先,我们看看前序.中序.后序遍历的特性: 前序遍历:     1.访问根节点     2.前序遍历左子树     3.前序遍历右子树 中序遍历:     1.中序遍历左子树     2.访问根节点     3.中序遍历右子树 后序遍历:     1.后序遍历左子树     2.后序遍历右子树     3.访问

已知面积求周长

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this pap

已知点和线, 求垂足

已知点 \(A(x_1, y_1)\), 现有一条直线 \(l\): \(ax+by+c=0\), 直线 \(l\) 到\(A\) 的举例为 \(d\), 点 \(A\) 到直线 \(l\) 的垂足点记为 \(B\), 求 \(B\) 的坐标. 解答 已知直线的表达式, 可知直线上的一个垂线量为: \((a,b)\), 记 \(B\) 的坐标表示为 \((x_2,y_2)\), 那么向量 \(\overrightarrow{BA} = (x_1 - x_2, y_1 - y_2)\), \(d(

ACM已知面积求最小周长

Description There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper. Input In the first line, there is an integer T indicates the number of te

睡觉前请关灯的 破解尝试版本 由已知解求一个矩阵的步骤

#include<iostream> #include"wz.h" #include<ctime> using namespace std; #define  MAX  5 void show(int arr[][MAX]) {  for(int i=0;i<MAX;i++)  {   for(int j=0;j<MAX;j++)   {    cout<<arr[i][j]<< " ";   }   co

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

Pashmak has fallen in love with an attractive girl called Parmida since one year ago... Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden

一直两个坐标点求旋转角度,或已知角度求移动的方向和位置

namespace Pioneer { export class Game extends Behaviour{ private nState:number; private Camera:Camera2D; nScore:number; nGold:number; private nRotationAngle:number = 0; private nRotationCount:number = 0; private nFrameRateTimer:number; private nScore

在平面内,已知一个矩形的四个角坐标,将矩形绕中心点转动一个角度,求旋转后的角坐标.

在平面内,已知一个矩形的四个角坐标,将矩形绕中心点转动一个角度,求旋转后的角坐标.也就是已知半径,求每个点旋转后的坐标. 把旋转前和旋转后的点加上中心点看成一个等腰三角形就好解决了,不用扇形公式,而是用三角形公式.假设矩形的左上角为(left, top),右下角为(right, bottom),则矩形上任意点(x0, y0)绕其中心(xcenter,ycenter)逆时针旋转angle角度后,新的坐标位置(x′, y′)的计算公式为: xcenter = (right - left + 1) /

已知正方形对角线两点求另外两点

正方形,已知 (x0,y0) 和(x2,y2)  可以根据下列关系求(x1,y1),(x3,y3) x1+x3 = x0+x2; x1-x3  =  y2-y0; y1+y3 =  y0+y2; y1-y3 =  x0-x2; node[0].p[1].x = ((node[0].p[0].x+node[0].p[2].x)+(node[0].p[2].y-node[0].p[0].y))/2; node[0].p[1].y = ((node[0].p[0].y+node[0].p[2].y)+