鱼C论坛

 找回密码
 立即注册
查看: 1987|回复: 4

随机数问题

[复制链接]
头像被屏蔽
发表于 2021-3-24 14:45:43 | 显示全部楼层 |阅读模式
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-24 15:19:12 | 显示全部楼层
本帖最后由 jackz007 于 2021-3-24 15:20 编辑
  1. import random

  2. while True:
  3.     s , d = 0 , []
  4.     for _ in range(50):
  5.         x = random . randint(3 , 5)
  6.         d . append(x)
  7.         s += x
  8.     if s == 185:
  9.         break
  10. print(d)
复制代码

        运行实况
  1. D:\0002.Exercise\Python>python x.py
  2. [3, 3, 3, 3, 3, 4, 5, 3, 5, 4, 5, 3, 5, 5, 4, 4, 3, 3, 3, 3, 3, 3, 5, 4, 3, 4, 5, 5, 3, 5, 4, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 5, 3, 5, 3, 3, 5, 4, 3]

  3. D:\0002.Exercise\Python>python x.py
  4. [4, 3, 4, 3, 3, 4, 3, 5, 4, 4, 4, 5, 4, 5, 4, 3, 3, 3, 5, 4, 3, 3, 3, 3, 5, 3, 3, 5, 3, 5, 3, 3, 3, 4, 3, 5, 3, 3, 3, 5, 5, 3, 5, 3, 4, 3, 3, 4, 4, 3]

  5. D:\0002.Exercise\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-24 15:28:40 | 显示全部楼层
你这题有2种解法,一种是不管效率,直接程序解题,一种是提升效率数学方法解题
一、程序法
  1. import random

  2. def new():
  3.    '''执行50次随机数'''
  4.    temp = []
  5.    for i in range(50):
  6.       temp.append(random.randint(3,5))
  7.    return temp

  8. def check(list1):
  9.    '''检查列表的和'''
  10.    temp = 0
  11.    for each in list1:
  12.       temp += each
  13.    return temp == 185

  14. while True:
  15.    list1 = new()
  16.    if check(list1):
  17.       print('大功告成')
  18.       print(list1)
  19.       break
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-24 15:33:27 | 显示全部楼层
数学法:
全5和3情况:
50*5-185=40,除以差额2,等于20
也就是最多只能有30个5

全4和3情况:
50*4-185=15,除以差额1,等于15
最多只能有35个4
--------------知道这2个逻辑以后,在随机生成列表的时候,每插入一个随机数,就检查下其中4和5的数量是否超上限,超过则自动重新生成,效率会稍微高点
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-24 15:48:04 | 显示全部楼层
本帖最后由 python初学者021 于 2021-3-24 15:51 编辑

我写的代码

原理
1:先产生一个列表,包括50个4
2:因为sum是185,所以先将 15个元素从4换成3,满足和是185
3:在剩下的4中,随机选出N组(2个4元素),将 4,4 换成 3,5
        这一步保证sum不变


  1. #!/usr/bin/python3

  2. import random

  3. return_list = [4] * 50

  4. diff = 4 * 50 - 185

  5. while diff:
  6.         choiced_index = random.randint(0,49)
  7.         if return_list[choiced_index] == 4:
  8.                 return_list[choiced_index] = 3
  9.                 diff -= 1

  10. number_of_4 = return_list.count(4)

  11. number_of_switch = random.randint(0,int(number_of_4/2))

  12. while number_of_switch:
  13.         NO_find_flag = True
  14.         while NO_find_flag:
  15.                 change_index1 = random.randint(0,49)
  16.                 if return_list[change_index1] == 4:
  17.                         NO_find_flag = False

  18.         NO_find_flag = True
  19.         while NO_find_flag:
  20.                 change_index2 = random.randint(0,49)
  21.                 if (change_index1 != change_index2) & (return_list[change_index2] == 4):
  22.                         NO_find_flag = False

  23.         return_list[change_index1] = 3
  24.         return_list[change_index2] = 5
  25.         number_of_switch -= 1

  26. print(return_list)
复制代码


result:
[3, 4, 3, 4, 4, 3, 3, 4, 3, 5, 5, 3, 5, 4, 4, 4, 3, 5, 4, 3, 3, 3, 5, 3, 4, 3, 4, 3, 4, 5, 3, 3, 3, 4, 3, 3, 5, 3, 3, 4, 3, 3, 3, 5, 5, 5, 3, 4, 4, 3]
[4, 3, 3, 3, 4, 4, 4, 3, 4, 5, 3, 4, 4, 3, 4, 4, 4, 4, 4, 5, 4, 4, 4, 3, 4, 4, 4, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 4, 4, 3, 3, 4, 3, 4, 3, 4, 3, 4, 4]


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 08:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表