2017-01-26 5 views
0

내가 그 시간 규칙에 대한 최소/최대 비율에 따라 백분율로 하루를 통해 몇 가지 숫자를 펼쳐 알고리즘으로 필요 확산이 내가 기압이 무엇 :문제가 더 나은 무작위 알고리즘을 만들어 매일

array:5 [ 
    "00-07" => 5 
    "08-12" => 24 
    "13-18" => 30 
    "19-22" => 32 
    "23-24" => 9 
] 
array:5 [ 
    "00-07" => 8 
    "08-12" => 29 
    "13-18" => 29 
    "19-22" => 27 
    "23-24" => 7 
] 
array:5 [ 
    "00-07" => 8 
    "08-12" => 28 
    "13-18" => 21 
    "19-22" => 33 
    "23-24" => 10 
] 
array:5 [ 
    "00-07" => 9 
    "08-12" => 20 
    "13-18" => 21 
    "19-22" => 25 
    "23-24" => 15 
] 
array:5 [ 
    "00-07" => 8 
    "08-12" => 28 
    "13-18" => 20 
    "19-22" => 29 
    "23-24" => 10 
] 
array:5 [ 
    "00-07" => 9 
    "08-12" => 20 
    "13-18" => 22 
    "19-22" => 28 
    "23-24" => 15 
] 
array:5 [ 
    "00-07" => 5 
    "08-12" => 23 
    "13-18" => 23 
    "19-22" => 40 
    "23-24" => 9 
] 

이를 최적화하고 더 "랜덤", ID가 최소, 최대 규칙을 증가시킬 수 있기를 만들지 만, 내가이 작업을 수행 할 경우, 최종 요소는 항상 고통, 그리고 할 수있는 방법은 0이된다

ID는 실제로는 시간별로 작성하는 것이 아니라 시간별로 작성하는 것이 가장 좋습니다.

+0

당신은 ['에는, mt_rand()'(http://www.php.net/manual/en/function.mt를 사용할 수 있습니다 -rand.php) 더 나은 임의성 – Xorifelse

답변

1

적절한 조건부 확률을 사용해야합니다. 파이썬 죄송합니다 3.

import collections 
import random 


def sample(total, intervals): 
    counts = [collections.Counter({0: 1})] 
    for interval in reversed(intervals): 
     counts.append(collections.Counter()) 
     for x in interval: 
      for tot, n in counts[-2].items(): 
       counts[-1][x + tot] += n 
    samp = [] 
    for interval in intervals: 
     outcome = random.randrange(counts.pop()[total]) 
     for x in interval: 
      outcome -= counts[-1][total - x] 
      if outcome < 0: 
       samp.append(x) 
       break 
     else: 
      assert False 
     total -= samp[-1] 
    return samp 


for i in range(20): 
    print(sample(100, [ 
     range(5, 11), 
     range(20, 31), 
     range(20, 31), 
     range(25, 41), 
     range(10, 16), 
    ])) 

샘플 출력 :

[10, 21, 20, 39, 10] 
[7, 27, 26, 29, 11] 
[6, 28, 21, 31, 14] 
[7, 22, 27, 34, 10] 
[10, 21, 29, 27, 13] 
[9, 23, 24, 34, 10] 
[7, 24, 30, 26, 13] 
[7, 22, 20, 39, 12] 
[10, 28, 25, 27, 10] 
[9, 22, 20, 38, 11] 
[5, 26, 30, 29, 10] 
[7, 25, 22, 33, 13] 
[5, 27, 21, 37, 10] 
[9, 23, 25, 28, 15] 
[6, 28, 25, 28, 13] 
[7, 30, 23, 30, 10] 
[7, 21, 20, 38, 14] 
[6, 26, 22, 34, 12] 
[8, 24, 23, 30, 15] 
[9, 29, 25, 25, 12]