Skip to content

Instantly share code, notes, and snippets.

@andijakl
Created June 18, 2021 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andijakl/637592b254952819e52fddd0bd993dd4 to your computer and use it in GitHub Desktop.
Save andijakl/637592b254952819e52fddd0bd993dd4 to your computer and use it in GitHub Desktop.
Generate unique random numbers in a custom code block for Voiceflow
// 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