Created
June 18, 2021 09:44
-
-
Save andijakl/637592b254952819e52fddd0bd993dd4 to your computer and use it in GitHub Desktop.
Generate unique random numbers in a custom code block for Voiceflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define number of choices available. | |
// Generated options: 0.. < num_max | |
const num_max = 5; | |
// Randomize array in-place using Durstenfeld shuffle algorithm | |
// Based on: https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array | |
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; | |
} | |
} | |
if (num_available === 0 || num_available.length === 0) { | |
// First run / no numbers left | |
// -> Fill array with shuffled options | |
num_available = Array.from(Array(num_max).keys()); | |
shuffleArray(num_available); | |
} | |
// Select random option | |
num_now = num_available.pop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment