数字生成器 [英] number generator

查看:70
本文介绍了数字生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须生成一个N个随机数(整数)的列表,其总和是

等于M.例如,我必须生成5个随机数,其中

sum是50,可能的解决方案可能是[3,11,7,22,7]。在Python中是否有一个简单的模式或函数来实现它?


感谢和问候

Francesco

I have to generate a list of N random numbers (integer) whose sum is
equal to M. If, for example, I have to generate 5 random numbers whose
sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
simple pattern or function in Python to accomplish that?

Thanks and regards
Francesco

推荐答案

" cesco" < fd ********** @ gmail.comwrites:
"cesco" <fd**********@gmail.comwrites:

我必须生成一个N个随机数(整数)的列表,其总和是

等于M.例如,如果我必须生成5个随机数,其中

之和为50,则可能的解决方案可能是[3,11,7,22, 7]。在Python中是否有一个简单的模式或函数来完成它?
I have to generate a list of N random numbers (integer) whose sum is
equal to M. If, for example, I have to generate 5 random numbers whose
sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
simple pattern or function in Python to accomplish that?



嗯,是的,很多方法,可能会对

问题进一步限制,例如整数的大小,如何这些名单应该是

待分发等等。你能更具体一点吗?这是一个

申请表吗?如果这是一个家庭作业问题,那很好,但在这种情况下更好

对于受访者来说建议提示而不是完整的解决方案。

Erm, yes, lots of ways, there are probably further constraints on the
problem, such as the size of the integers, how the lists are supposed
to be distributed, etc. Can you be more specific? Is this for an
application? If it''s a homework problem, that''s fine, but it''s better
in that case for respondents to suggest hints rather than full solutions.


3月9日下午3:51,Paul Rubin< http://phr...@NOSPAM.invalidwrote:
On Mar 9, 3:51 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:

" CESCO" < fd.calabr ... @ gmail.comwrites:
"cesco" <fd.calabr...@gmail.comwrites:

我必须生成一个N个随机数(整数)的列表,其总和为

等于M.例如,如果我必须生成5个随机数,其中

之和为50,则可能的解决方案可能是[3,11,7,22,7]。在Python中是否有一个简单的模式或函数来完成它?
I have to generate a list of N random numbers (integer) whose sum is
equal to M. If, for example, I have to generate 5 random numbers whose
sum is 50 a possible solution could be [3, 11, 7, 22, 7]. Is there a
simple pattern or function in Python to accomplish that?



嗯,是的,很多方法,可能会对

问题进一步限制,例如整数的大小,如何这些名单应该是

待分发等等。你能更具体一点吗?这是一个

申请表吗?如果这是一个家庭作业问题,那很好,但在这种情况下,对于受访者来说建议提示而不是完整解决方案更好。


Erm, yes, lots of ways, there are probably further constraints on the
problem, such as the size of the integers, how the lists are supposed
to be distributed, etc. Can you be more specific? Is this for an
application? If it''s a homework problem, that''s fine, but it''s better
in that case for respondents to suggest hints rather than full solutions.



给定两个正整数,N和M,其中N < M,我必须生成N

正整数,使得sum(N)= M.没有更多限制。


再次感谢

Francesco

Given two positive integers, N and M with N < M, I have to generate N
positive integers such that sum(N)=M. No more constraints.

Thanks again
Francesco


In< 11 **********************@j27g2000cwj.googlegroups .com> ;, cesco写道:
In <11**********************@j27g2000cwj.googlegroups .com>, cesco wrote:

鉴于两个正整数,N和M,N < M,我必须生成N

正整数,使得sum(N)= M.没有更多限制。
Given two positive integers, N and M with N < M, I have to generate N
positive integers such that sum(N)=M. No more constraints.



将其分解为子问题。从合适的

范围生成随机数X,并留下一个数字,并生成(N-1)

随机数加起来的问题( MX)。你必须要考虑一下适当的范围

。但是,
绘制随机数的必要函数在`random`模块中。


Ciao,

Marc''BlackJack''Rintsch

Break it into subproblems. Generate a random number X from a suitable
range and you are left with one number, and the problem to generate (N-1)
random numbers that add up to (M-X). You have to think a little bit about
the "suitable range" part though.

The necessary functions to draw random numbers are in the `random` module.

Ciao,
Marc ''BlackJack'' Rintsch


这篇关于数字生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆