# Week 4 Homework

질문 1
3개의 작업과 난수 시드 1, 2, 3으로 시뮬레이션한 솔루션을 계산하세요.

해설

```sh
  --> Solutions
Seed 1
Random 951249 -> Winning ticket 9 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:15 tix:1 ) (  job:1 timeleft:3 tix:6 ) (* job:2 timeleft:5 tix:8 )
Random 210933 -> Winning ticket 13 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:14 tix:1 ) (  job:1 timeleft:3 tix:6 ) (* job:2 timeleft:4 tix:8 )
Random 295733 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:14 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:4 tix:8 )
Random 876194 -> Winning ticket 14 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:13 tix:1 ) (  job:1 timeleft:3 tix:6 ) (* job:2 timeleft:3 tix:8 )
Random 969331 -> Winning ticket 11 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:12 tix:1 ) (  job:1 timeleft:3 tix:6 ) (* job:2 timeleft:2 tix:8 )
Random 495767 -> Winning ticket 7 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:12 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 270546 -> Winning ticket 6 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:11 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 793533 -> Winning ticket 13 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:10 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 241684 -> Winning ticket 4 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:9 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 437216 -> Winning ticket 1 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:8 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 223530 -> Winning ticket 13 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:7 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 565567 -> Winning ticket 2 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:6 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 108313 -> Winning ticket 2 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:5 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:2 tix:8 )
Random 764987 -> Winning ticket 14 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:4 tix:1 ) (  job:1 timeleft:3 tix:6 ) (* job:2 timeleft:1 tix:8 )
Random 254440 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:4 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:1 tix:8 )
Random 759059 -> Winning ticket 4 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:3 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:1 tix:8 )
Random 424048 -> Winning ticket 3 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:2 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:1 tix:8 )
Random 870783 -> Winning ticket 3 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:1 tix:1 ) (  job:1 timeleft:3 tix:6 ) (  job:2 timeleft:1 tix:8 )
--> JOB 2 DONE at time 20
Random 602040 -> Winning ticket 7 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:1 tix:1 ) (* job:1 timeleft:2 tix:6 ) (  job:2 timeleft:0 tix:8 )
--> JOB 1 DONE at time 22
Random 619437 -> Winning ticket 8 (of 9) -> Run 0
  Jobs:  (* job:0 timeleft:1 tix:1 ) (  job:1 timeleft:1 tix:6 ) (  job:2 timeleft:0 tix:--- )
--> JOB 0 DONE at time 23
```

```sh
  --> Solutions
Seed 2
Random 254426 -> Winning ticket 10 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:15 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:5 tix:10 )
Random 520649 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:15 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:5 tix:10 )
Random 215857 -> Winning ticket 6 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:14 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:5 tix:10 )
Random 229234 -> Winning ticket 3 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:13 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:5 tix:10 )
Random 771727 -> Winning ticket 10 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:12 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:4 tix:10 )
Random 742908 -> Winning ticket 11 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:11 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:3 tix:10 )
Random 216838 -> Winning ticket 9 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:10 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:2 tix:10 )
Random 295738 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:10 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:2 tix:10 )
Random 222042 -> Winning ticket 7 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:9 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:2 tix:10 )
Random 186460 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:9 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:2 tix:10 )
Random 548720 -> Winning ticket 11 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:8 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:2 tix:10 )
Random 720491 -> Winning ticket 12 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:7 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:2 tix:10 )
Random 901892 -> Winning ticket 14 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:6 tix:1 ) (  job:1 timeleft:5 tix:4 ) (* job:2 timeleft:1 tix:10 )
--> JOB 2 DONE at time 20
Random 429540 -> Winning ticket 8 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:6 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
Random 272157 -> Winning ticket 2 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:5 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
Random 934294 -> Winning ticket 12 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:4 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
Random 254517 -> Winning ticket 4 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:4 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
Random 438908 -> Winning ticket 8 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:3 tix:1 ) (  job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
Random 441490 -> Winning ticket 11 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:2 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
Random 496110 -> Winning ticket 7 (of 15) -> Run 1
  Jobs:  (  job:0 timeleft:1 tix:1 ) (* job:1 timeleft:5 tix:4 ) (  job:2 timeleft:1 tix:10 )
--> JOB 1 DONE at time 27
Random 604754 -> Winning ticket 8 (of 10) -> Run 0
  Jobs:  (* job:0 timeleft:1 tix:1 ) (  job:1 timeleft:4 tix:--- ) (  job:2 timeleft:1 tix:10 )
--> JOB 0 DONE at time 28
```

```sh
--> Solutions
Seed 3
Random 245846 -> Winning ticket 11 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:15 tix:1 ) (  job:1 timeleft:2 tix:2 ) (* job:2 timeleft:9 tix:12 )
Random 701938 -> Winning ticket 1 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:15 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:9 tix:12 )
Random 596812 -> Winning ticket 2 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:14 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:9 tix:12 )
Random 362375 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:13 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:9 tix:12 )
Random 938265 -> Winning ticket 1 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:12 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:9 tix:12 )
Random 685574 -> Winning ticket 14 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:11 tix:1 ) (  job:1 timeleft:2 tix:2 ) (* job:2 timeleft:8 tix:12 )
Random 877592 -> Winning ticket 7 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:11 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:8 tix:12 )
Random 345646 -> Winning ticket 11 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:10 tix:1 ) (  job:1 timeleft:2 tix:2 ) (* job:2 timeleft:7 tix:12 )
Random 946501 -> Winning ticket 7 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:10 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:7 tix:12 )
Random 617085 -> Winning ticket 14 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:9 tix:1 ) (  job:1 timeleft:2 tix:2 ) (* job:2 timeleft:6 tix:12 )
Random 999556 -> Winning ticket 6 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:9 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:6 tix:12 )
Random 508824 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:8 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:6 tix:12 )
Random 703026 -> Winning ticket 14 (of 15) -> Run 2
  Jobs:  (  job:0 timeleft:7 tix:1 ) (  job:1 timeleft:2 tix:2 ) (* job:2 timeleft:5 tix:12 )
Random 978014 -> Winning ticket 8 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:7 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:5 tix:12 )
Random 603682 -> Winning ticket 4 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:6 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:5 tix:12 )
Random 177765 -> Winning ticket 2 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:5 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:5 tix:12 )
Random 783928 -> Winning ticket 5 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:4 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:5 tix:12 )
Random 209550 -> Winning ticket 3 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:3 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:5 tix:12 )
Random 569977 -> Winning ticket 3 (of 15) -> Run 0
  Jobs:  (* job:0 timeleft:2 tix:1 ) (  job:1 timeleft:2 tix:2 ) (  job:2 timeleft:5 tix:12 )
Random 178105 -> Winning ticket 8 (of 12) -> Run 0
  Jobs:  (* job:0 timeleft:1 tix:1 ) (  job:1 timeleft:1 tix:2 ) (  job:2 timeleft:5 tix:--- )
--> JOB 0 DONE at time 21
Random 204527 -> Winning ticket 5 (of 10) -> Run 1
  Jobs:  (  job:0 timeleft:1 tix:1 ) (* job:1 timeleft:0 tix:2 ) (  job:2 timeleft:5 tix:--- )
--> JOB 1 DONE at time 22
```

질문 2
2.1 이제 두 개의 특정 작업으로 실행해 보세요: 각각 길이는 10이지만 하나(작업 0)는 티켓이 1장이고 다른 하나(작업 1)는 100장입니다(예: `-l 10:1,10:100`).
해설

```sh
./lottery.py -j 2 -l 10:1,10:100 -c
```

```sh
  --> Solutions
Random 764209 -> Winning ticket 6 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:10 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 298930 -> Winning ticket 29 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:9 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 791037 -> Winning ticket 78 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:8 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 582670 -> Winning ticket 83 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:7 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 675389 -> Winning ticket 18 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:6 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 132784 -> Winning ticket 29 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:5 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 777972 -> Winning ticket 2 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:4 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 619922 -> Winning ticket 44 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:3 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 572304 -> Winning ticket 1 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:2 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 708558 -> Winning ticket 67 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:1 tix:1 ) (* job:1 timeleft:10 tix:100 )
Random 569222 -> Winning ticket 80 (of 101) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:1 ) (* job:1 timeleft:10 tix:100 )
--> JOB 0 DONE at time 11
Random 618389 -> Winning ticket 21 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:9 tix:100 )
Random 246083 -> Winning ticket 83 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:8 tix:100 )
Random 408327 -> Winning ticket 27 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:7 tix:100 )
Random 254006 -> Winning ticket 6 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:6 tix:100 )
Random 966337 -> Winning ticket 38 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:5 tix:100 )
Random 349256 -> Winning ticket 81 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:4 tix:100 )
Random 337061 -> Winning ticket 33 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:3 tix:100 )
Random 309474 -> Winning ticket 74 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:2 tix:100 )
Random 269628 -> Winning ticket 99 (of 100) -> Run 1
  Jobs:  (  job:0 timeleft:0 tix:--- ) (* job:1 timeleft:1 tix:100 )
--> JOB 1 DONE at time 20
```

2.2 티켓의 수가 이렇게 불균형할 때 어떤 일이 일어날까요?
해설 - 티켓의 불균형이 큰 경우, 티켓이 많은 작업이 CPU 시간을 지배할 수 있는 위험이 있습니다. 이는 공평하지 않은 실행 시간 할당을 초래할 수 있습니다.

2.3 작업 1이 완료되기 전에 작업 0이 실행될 수 있을까요?
해설 - 복권 스케줄링 알고리즘에서는 CPU 시간을 티켓을 가진 작업 중에서 랜덤하게 선택하여 할당합니다. 따라서 작업 1이 완료되기 전에 작업 0이 실행될 수 있다는 것은 가능하지만, 그 확률은 작업 1이 실행될 확률에 비해 매우 낮을 것입니다.

2.4 얼마나 자주 그럴까요?
해설 - 작업 0이 작업 1보다 빠르게 선택되어 CPU를 얻을 확률은 작업 0에 할당된 티켓 수와 작업 1에 할당된 티켓 수의 비율에 의해 결정됩니다. 일반적으로 작업 0이 작업 1보다 먼저 CPU를 얻는 일은 매우 드물 것입니다.

2.5 일반적으로 이러한 티켓 불균형이 복권 스케줄링의 동작에 어떤 영향을 미칠까요?
해설 - 티켓의 불균형은 복권 스케줄링 알고리즘의 효율성과 공평성에 영향을 줄 수 있으며, 이를 해결하기 위해 작업 간 티켓의 균형을 유지하는 것이 중요합니다.
