Repository: TheAlgorithms/Java
Pull request #5656 (2024-10-09 05:32:18)

Add `LotteryScheduling` new algorithm with Junit tests
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5656?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `81.39535%` with `8 lines` in your changes missing coverage. Please review.
> Project coverage is 58.65%. Comparing base [(`b54cc21`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b54cc21ade112e20f429964dc86f8543a5605d98?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`1160b87`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/1160b8703659f4327fb565bceb8f7430bfb8d54c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5656?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...om/thealgorithms/scheduling/LotteryScheduling.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5656?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fscheduling%2FLotteryScheduling.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9zY2hlZHVsaW5nL0xvdHRlcnlTY2hlZHVsaW5nLmphdmE=) | 81.39% | [6 Missing and 2 partials :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5656?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5656      +/-   ##
============================================
+ Coverage     58.59%   58.65%   +0.06%     
- Complexity     3749     3753       +4     
============================================
  Files           554      555       +1     
  Lines         15922    15965      +43     
  Branches       3030     3033       +3     
============================================
+ Hits           9330     9365      +35     
- Misses         6195     6201       +6     
- Partials        397      399       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5656?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

siriak: Please fix infer PR check
Hardvan: I think the infer check is failing because I added a new dependency (mockito) in the pom.xml file. Mockito is needed for mocking the random number generator in the test cases.
siriak: It says
src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java:56: error: Null Dereference(NULLPTR_DEREFERENCE)
  `selectedProcess` could be null (from the call to `LotteryScheduling.selectProcessByTicket(...)` on line 54) and is dereferenced. 
  54.               Process selectedProcess = selectProcessByTicket(winningTicket);
  55.   
  56. >             selectedProcess.setWaitingTime(currentTime);
  57.               currentTime += selectedProcess.getBurstTime();
  58.               selectedProcess.setTurnAroundTime(selectedProcess.getWaitingTime() + selectedProcess.getBurstTime());
Hardvan: I have fixed the bug.

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index f1a53148..19301ee3 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -504,6 +504,7 @@
             * [FCFSScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/FCFSScheduling.java)
             * [HighestResponseRatioNextScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/HighestResponseRatioNextScheduling.java)
             * [JobSchedulingWithDeadline](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/JobSchedulingWithDeadline.java)
+            * [LotteryScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java)
             * [MLFQScheduler](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/MLFQScheduler.java)
             * [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
             * [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
@@ -1039,6 +1040,7 @@
             * [FCFSSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/FCFSSchedulingTest.java)
             * [HighestResponseRatioNextSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/HighestResponseRatioNextSchedulingTest.java)
             * [JobSchedulingWithDeadlineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/JobSchedulingWithDeadlineTest.java)
+            * [LotterySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java)
             * [MLFQSchedulerTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/MLFQSchedulerTest.java)
             * [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
             * [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
diff --git a/pom.xml b/pom.xml
index 673be6cd..812f46c7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,6 +40,13 @@
             <version>${assertj.version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>5.14.1</version>
+            <scope>test</scope>
+        </dependency>
+
 
         <dependency>
             <groupId>org.junit.jupiter</groupId>
diff --git a/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java b/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java
new file mode 100644
index 00000000..cea0c793
--- /dev/null
+++ b/src/main/java/com/thealgorithms/scheduling/LotteryScheduling.java
@@ -0,0 +1,141 @@
+package com.thealgorithms.scheduling;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * The LotteryScheduling class implements the Lottery Scheduling algorithm, which is
+ * a probabilistic CPU scheduling algorithm. Processes are assigned tickets, and
+ * the CPU is allocated to a randomly selected process based on ticket count.
+ * Processes with more tickets have a higher chance of being selected.
+ */
+public final class LotteryScheduling {
+    private LotteryScheduling() {
+    }
+
+    private List<Process> processes;
+    private Random random;
+
+    /**
+     * Constructs a LotteryScheduling object with the provided list of processes.
+     *
+     * @param processes List of processes to be scheduled using Lottery Scheduling.
+     */
+    public LotteryScheduling(final List<Process> processes) {
+        this.processes = processes;
+        this.random = new Random();
+    }
+
+    /**
+     * Constructs a LotteryScheduling object with the provided list of processes and a Random object.
+     *
+     * @param processes List of processes to be scheduled using Lottery Scheduling.
+     * @param random    Random object used for generating random numbers.
+     */
+    public LotteryScheduling(final List<Process> processes, Random random) {
+        this.processes = processes;
+        this.random = random;
+    }
+
+    /**
+     * Schedules the processes using the Lottery Scheduling algorithm.
+     * Each process is assigned a certain number of tickets, and the algorithm randomly
+     * selects a process to execute based on ticket count. The method calculates the
+     * waiting time and turnaround time for each process and simulates their execution.
+     */
+    public List<Process> scheduleProcesses() {
+        int totalTickets = processes.stream().mapToInt(Process::getTickets).sum();
+        int currentTime = 0;
+        List<Process> executedProcesses = new ArrayList<>();
+
+        while (!processes.isEmpty()) {
+            int winningTicket = random.nextInt(totalTickets) + 1;
+            Process selectedProcess = selectProcessByTicket(winningTicket);
+
+            if (selectedProcess == null) {
+                // This should not happen in normal circumstances, but we'll handle it just in case
+                System.err.println("Error: No process selected. Recalculating total tickets.");
+                totalTickets = processes.stream().mapToInt(Process::getTickets).sum();
+                continue;
+            }
+
+            selectedProcess.setWaitingTime(currentTime);
+            currentTime += selectedProcess.getBurstTime();
+            selectedProcess.setTurnAroundTime(selectedProcess.getWaitingTime() + selectedProcess.getBurstTime());
+
+            executedProcesses.add(selectedProcess);
+            processes.remove(selectedProcess);
+
+            totalTickets = processes.stream().mapToInt(Process::getTickets).sum();
+        }
+
+        return executedProcesses;
+    }
+
+    /**
+     * Selects a process based on a winning ticket. The method iterates over the
+     * list of processes, and as the ticket sum accumulates, it checks if the
+     * current process holds the winning ticket.
+     *
+     * @param winningTicket The randomly generated ticket number that determines the selected process.
+     * @return The process associated with the winning ticket.
+     */
+    private Process selectProcessByTicket(int winningTicket) {
+        int ticketSum = 0;
+        for (Process process : processes) {
+            ticketSum += process.getTickets();
+            if (ticketSum >= winningTicket) {
+                return process;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * The Process class represents a process in the scheduling system. Each process has
+     * an ID, burst time (CPU time required for execution), number of tickets (used in
+     * lottery selection), waiting time, and turnaround time.
+     */
+    public static class Process {
+        private String processId;
+        private int burstTime;
+        private int tickets;
+        private int waitingTime;
+        private int turnAroundTime;
+
+        public Process(String processId, int burstTime, int tickets) {
+            this.processId = processId;
+            this.burstTime = burstTime;
+            this.tickets = tickets;
+        }
+
+        public String getProcessId() {
+            return processId;
+        }
+
+        public int getBurstTime() {
+            return burstTime;
+        }
+
+        public int getTickets() {
+            return tickets;
+        }
+
+        public int getWaitingTime() {
+            return waitingTime;
+        }
+
+        public void setWaitingTime(int waitingTime) {
+            this.waitingTime = waitingTime;
+        }
+
+        public int getTurnAroundTime() {
+            return turnAroundTime;
+        }
+
+        public void setTurnAroundTime(int turnAroundTime) {
+            this.turnAroundTime = turnAroundTime;
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java b/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java
new file mode 100644
index 00000000..00fd8adc
--- /dev/null
+++ b/src/test/java/com/thealgorithms/scheduling/LotterySchedulingTest.java
@@ -0,0 +1,64 @@
+package com.thealgorithms.scheduling;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class LotterySchedulingTest {
+
+    private Random mockRandom;
+
+    @BeforeEach
+    public void setup() {
+        mockRandom = mock(Random.class);
+    }
+
+    @Test
+    public void testLotterySchedulingWithMockedRandom() {
+        List<LotteryScheduling.Process> processes = createProcesses();
+        LotteryScheduling lotteryScheduling = new LotteryScheduling(processes, mockRandom);
+
+        // Mock the sequence of random numbers (winning tickets)
+        // This sequence ensures that P1 (10 tickets), P3 (8 tickets), and P2 (5 tickets) are selected.
+        when(mockRandom.nextInt(23)).thenReturn(5, 18, 11); // winning tickets for P1, P3, and P2
+
+        List<LotteryScheduling.Process> executedProcesses = lotteryScheduling.scheduleProcesses();
+
+        assertEquals(3, executedProcesses.size());
+
+        // Assert the process execution order and properties
+        LotteryScheduling.Process process1 = executedProcesses.get(0);
+        assertEquals("P1", process1.getProcessId());
+        assertEquals(0, process1.getWaitingTime());
+        assertEquals(10, process1.getTurnAroundTime());
+
+        LotteryScheduling.Process process2 = executedProcesses.get(1);
+        assertEquals("P2", process2.getProcessId());
+        assertEquals(10, process2.getWaitingTime());
+        assertEquals(15, process2.getTurnAroundTime());
+
+        LotteryScheduling.Process process3 = executedProcesses.get(2);
+        assertEquals("P3", process3.getProcessId());
+        assertEquals(15, process3.getWaitingTime());
+        assertEquals(23, process3.getTurnAroundTime());
+    }
+
+    private List<LotteryScheduling.Process> createProcesses() {
+        LotteryScheduling.Process process1 = new LotteryScheduling.Process("P1", 10, 10); // 10 tickets
+        LotteryScheduling.Process process2 = new LotteryScheduling.Process("P2", 5, 5); // 5 tickets
+        LotteryScheduling.Process process3 = new LotteryScheduling.Process("P3", 8, 8); // 8 tickets
+
+        List<LotteryScheduling.Process> processes = new ArrayList<>();
+        processes.add(process1);
+        processes.add(process2);
+        processes.add(process3);
+
+        return processes;
+    }
+}