Skip to content

feat: Add RandomScheduling new algorithm with Junit tests #5810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 29, 2024
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@
* [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)
* [ProportionalFairScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/ProportionalFairScheduling.java)
* [RandomScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RandomScheduling.java)
* [RRScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/RRScheduling.java)
* [SelfAdjustingScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SelfAdjustingScheduling.java)
* [SJFScheduling](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/scheduling/SJFScheduling.java)
Expand Down Expand Up @@ -1192,6 +1193,7 @@
* [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)
* [ProportionalFairSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/ProportionalFairSchedulingTest.java)
* [RandomSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RandomSchedulingTest.java)
* [RRSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/RRSchedulingTest.java)
* [SelfAdjustingSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SelfAdjustingSchedulingTest.java)
* [SJFSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/scheduling/SJFSchedulingTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.thealgorithms.scheduling;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
* RandomScheduling is an algorithm that assigns tasks in a random order.
* It doesn't consider priority, deadlines, or burst times, making it
* inefficient but useful in scenarios where fairness or unpredictability
* is required (e.g., load balancing in distributed systems).
*
* Use Case: Distributed systems where randomness helps avoid task starvation.
*
* @author Hardvan
*/
public final class RandomScheduling {

private final List<String> tasks;
private final Random random;

/**
* Constructs a new RandomScheduling instance.
*
* @param tasks A collection of task names to be scheduled.
* @param random A Random instance for generating random numbers.
*/
public RandomScheduling(Collection<String> tasks, Random random) {
this.tasks = new ArrayList<>(tasks);
this.random = random;
}

/**
* Schedules the tasks randomly and returns the randomized order.
*
* @return A list representing the tasks in their randomized execution order.
*/
public List<String> schedule() {
List<String> shuffledTasks = new ArrayList<>(tasks);
Collections.shuffle(shuffledTasks, random);
return shuffledTasks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.thealgorithms.scheduling;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Random;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class RandomSchedulingTest {

private RandomScheduling randomScheduling;
private Random mockRandom;

@BeforeEach
public void setup() {
mockRandom = mock(Random.class); // Mocking Random for predictable behavior
}

@Test
public void testRandomOrder1() {
// Arrange
List<String> tasks = List.of("Task1", "Task2", "Task3");
// Mock the random sequence to control shuffling: swap 0 <-> 1, and 1 <-> 2.
when(mockRandom.nextInt(anyInt())).thenReturn(1, 2, 0);
randomScheduling = new RandomScheduling(tasks, mockRandom);

// Act
List<String> result = randomScheduling.schedule();

// Assert
assertEquals(List.of("Task1", "Task2", "Task3"), result);
}

@Test
public void testRandomOrder2() {
// Arrange
List<String> tasks = List.of("A", "B", "C", "D");
// Mocking predictable swaps for the sequence: [C, B, D, A]
when(mockRandom.nextInt(anyInt())).thenReturn(2, 1, 3, 0);
randomScheduling = new RandomScheduling(tasks, mockRandom);

// Act
List<String> result = randomScheduling.schedule();

// Assert
assertEquals(List.of("A", "C", "B", "D"), result);
}

@Test
public void testSingleTask() {
// Arrange
List<String> tasks = List.of("SingleTask");
when(mockRandom.nextInt(anyInt())).thenReturn(0); // No real shuffle
randomScheduling = new RandomScheduling(tasks, mockRandom);

// Act
List<String> result = randomScheduling.schedule();

// Assert
assertEquals(List.of("SingleTask"), result);
}

@Test
public void testEmptyTaskList() {
// Arrange
List<String> tasks = List.of();
randomScheduling = new RandomScheduling(tasks, mockRandom);

// Act
List<String> result = randomScheduling.schedule();

// Assert
assertEquals(List.of(), result); // Should return an empty list
}

@Test
public void testSameTasksMultipleTimes() {
// Arrange
List<String> tasks = List.of("X", "X", "Y", "Z");
when(mockRandom.nextInt(anyInt())).thenReturn(3, 0, 1, 2);
randomScheduling = new RandomScheduling(tasks, mockRandom);

// Act
List<String> result = randomScheduling.schedule();

// Assert
assertEquals(List.of("Y", "X", "X", "Z"), result);
}
}