• 请不要在回答技术问题时复制粘贴 AI 生成的内容
baocool
V2EX  ›  程序员

EXCEL 随机数如何限定范围,并让其总和等于一个定值?

  •  1
     
  •   baocool · Sep 17, 2014 · 8600 views
    This topic created in 4264 days ago, the information mentioned may be changed or developed.
    需要10个随机数,总和为100,每个数在90和110的范围内

    目前使用
    Sub a()
    Range("b2:b11") = ""
    For i = 2 To 10
    Do Until WorksheetFunction.CountIf(Range("b:b"), Range("b" & i)) = 1 _
    And [b1] - WorksheetFunction.Sum(Range("b2:b" & i)) >= [E2] * (11 - i) _
    And [b1] - WorksheetFunction.Sum(Range("b2:b" & i)) <= [E3] * (11 - i)
    Range("b" & i) = Int(Rnd() * ([E3] - [E2])) + [E2]
    Loop
    Next i
    [b11] = [b1] - WorksheetFunction.Sum(Range("b2:b10"))
    End Sub


    但运行非常容易就导致excel 无法响应,求助
    4 replies    2014-09-17 12:16:08 +08:00
    yuguig
        1
    yuguig  
       Sep 17, 2014 via iPhone
    每个数90~110之间?好矛盾的限定……
    只需要9个随机数,之和小于100
    jybox
        2
    jybox  
       Sep 17, 2014
    这个思路如何:

    result = []

    for 0 .. 99
    pos = random(0, 9)

    if result[pos]
    result[pos] += 1
    else
    result[pos] = 1
    baocool
        4
    baocool  
    OP
       Sep 17, 2014
    实际需要并不特指10组数/和为100,所以,亲们,直接给个EXCEL 表吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3185 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 14:07 · PVG 22:07 · LAX 07:07 · JFK 10:07
    ♥ Do have faith in what you're doing.