-
Notifications
You must be signed in to change notification settings - Fork 909
Random Pair Generator Script #1711
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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** | ||
| - 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, yes I need to refactor this pull request. |
||
|
|
||
|
|
||
|
|
||
| outputs.match = randomPairs; | ||
|
|
||
| })(inputs, outputs); | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.