Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Random Pair Generator

## Overview
This script generates random pairs from an input array.
It is useful for creating **tournament matches**, **group assignments**, or **randomized pairings**.

The script is structured to run in a scoped execution context (such as **ServiceNow Flow Designer Actions** or **Scripted REST Transforms**) where it accepts `inputs` and `outputs` objects.

---

## How It Works
1. **Shuffle the Input Array**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this be enough to have random pairs? If you shuffle the array then f.e. 0 and 1 can be a random pain. What does the pushing the 2 pairs together in a paired array help do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a Function to take a list of players, in this case was from a Racing Competition Tournament app, where each car is represented by wheels and it would randomize the list of input sys ids and randomize a new order array. Then, from that new list order, it can generate pairs of head-to-head matches according to tournament rules preferences.

- Implements the Fisher–Yates algorithm in `shuffleArray()` to randomize the array.

2. **Create Random Pairs**
- Iterates through the shuffled array in steps of two.
- Forms pairs `[item1, item2]`.
- If the array has an odd length, the last item is placed in a single-element array `[item]`.

3. **Return Results**
- The final set of pairs is returned as `outputs.match`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(function execute(inputs, outputs) {
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}

function createRandomPairs(array) {
var shuffledArray = shuffleArray(array.slice()); // Make a copy of the array and shuffle it
var pairs = [];
for (var i = 0; i < shuffledArray.length; i += 2) {
if (i + 1 < shuffledArray.length) {
pairs.push([shuffledArray[i], shuffledArray[i + 1]]);
} else {
pairs.push([shuffledArray[i]]);
}
}
return pairs;
}


var randomPairs = createRandomPairs(inputs.wheels);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you expect an input with a "wheels" element, but this is not specified anywhere. Can you in the readme add the expected input format/instructions, or update this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, yes I need to refactor this pull request.
I need to better explain this function snippet and its use cases.




outputs.match = randomPairs;

})(inputs, outputs);
Loading