Skip to content

Commit e94be71

Browse files
authored
Add RandomScheduling algorithm (TheAlgorithms#5810)
1 parent d1c1e6b commit e94be71

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@
560560
* [NonPreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/NonPreemptivePriorityScheduling.java)
561561
* [PreemptivePriorityScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/PreemptivePriorityScheduling.java)
562562
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
563+
* [RandomScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RandomScheduling.java)
563564
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
564565
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
565566
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
@@ -1192,6 +1193,7 @@
11921193
* [NonPreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/NonPreemptivePrioritySchedulingTest.java)
11931194
* [PreemptivePrioritySchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/PreemptivePrioritySchedulingTest.java)
11941195
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
1196+
* [RandomSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RandomSchedulingTest.java)
11951197
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
11961198
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
11971199
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
/**
10+
* RandomScheduling is an algorithm that assigns tasks in a random order.
11+
* It doesn't consider priority, deadlines, or burst times, making it
12+
* inefficient but useful in scenarios where fairness or unpredictability
13+
* is required (e.g., load balancing in distributed systems).
14+
*
15+
* Use Case: Distributed systems where randomness helps avoid task starvation.
16+
*
17+
* @author Hardvan
18+
*/
19+
public final class RandomScheduling {
20+
21+
private final List<String> tasks;
22+
private final Random random;
23+
24+
/**
25+
* Constructs a new RandomScheduling instance.
26+
*
27+
* @param tasks A collection of task names to be scheduled.
28+
* @param random A Random instance for generating random numbers.
29+
*/
30+
public RandomScheduling(Collection<String> tasks, Random random) {
31+
this.tasks = new ArrayList<>(tasks);
32+
this.random = random;
33+
}
34+
35+
/**
36+
* Schedules the tasks randomly and returns the randomized order.
37+
*
38+
* @return A list representing the tasks in their randomized execution order.
39+
*/
40+
public List<String> schedule() {
41+
List<String> shuffledTasks = new ArrayList<>(tasks);
42+
Collections.shuffle(shuffledTasks, random);
43+
return shuffledTasks;
44+
}
45+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.thealgorithms.scheduling;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.Mockito.anyInt;
5+
import static org.mockito.Mockito.mock;
6+
import static org.mockito.Mockito.when;
7+
8+
import java.util.List;
9+
import java.util.Random;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class RandomSchedulingTest {
14+
15+
private RandomScheduling randomScheduling;
16+
private Random mockRandom;
17+
18+
@BeforeEach
19+
public void setup() {
20+
mockRandom = mock(Random.class); // Mocking Random for predictable behavior
21+
}
22+
23+
@Test
24+
public void testRandomOrder1() {
25+
// Arrange
26+
List<String> tasks = List.of("Task1", "Task2", "Task3");
27+
// Mock the random sequence to control shuffling: swap 0 <-> 1, and 1 <-> 2.
28+
when(mockRandom.nextInt(anyInt())).thenReturn(1, 2, 0);
29+
randomScheduling = new RandomScheduling(tasks, mockRandom);
30+
31+
// Act
32+
List<String> result = randomScheduling.schedule();
33+
34+
// Assert
35+
assertEquals(List.of("Task1", "Task2", "Task3"), result);
36+
}
37+
38+
@Test
39+
public void testRandomOrder2() {
40+
// Arrange
41+
List<String> tasks = List.of("A", "B", "C", "D");
42+
// Mocking predictable swaps for the sequence: [C, B, D, A]
43+
when(mockRandom.nextInt(anyInt())).thenReturn(2, 1, 3, 0);
44+
randomScheduling = new RandomScheduling(tasks, mockRandom);
45+
46+
// Act
47+
List<String> result = randomScheduling.schedule();
48+
49+
// Assert
50+
assertEquals(List.of("A", "C", "B", "D"), result);
51+
}
52+
53+
@Test
54+
public void testSingleTask() {
55+
// Arrange
56+
List<String> tasks = List.of("SingleTask");
57+
when(mockRandom.nextInt(anyInt())).thenReturn(0); // No real shuffle
58+
randomScheduling = new RandomScheduling(tasks, mockRandom);
59+
60+
// Act
61+
List<String> result = randomScheduling.schedule();
62+
63+
// Assert
64+
assertEquals(List.of("SingleTask"), result);
65+
}
66+
67+
@Test
68+
public void testEmptyTaskList() {
69+
// Arrange
70+
List<String> tasks = List.of();
71+
randomScheduling = new RandomScheduling(tasks, mockRandom);
72+
73+
// Act
74+
List<String> result = randomScheduling.schedule();
75+
76+
// Assert
77+
assertEquals(List.of(), result); // Should return an empty list
78+
}
79+
80+
@Test
81+
public void testSameTasksMultipleTimes() {
82+
// Arrange
83+
List<String> tasks = List.of("X", "X", "Y", "Z");
84+
when(mockRandom.nextInt(anyInt())).thenReturn(3, 0, 1, 2);
85+
randomScheduling = new RandomScheduling(tasks, mockRandom);
86+
87+
// Act
88+
List<String> result = randomScheduling.schedule();
89+
90+
// Assert
91+
assertEquals(List.of("Y", "X", "X", "Z"), result);
92+
}
93+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy