-1

I'm working on a project in which I need to generate sixteen random elements of an array. I need to ensure that exactly two of the random ones are a given element, but I don't want them to be in the same place every time. Is there a way to generate random items from an array but with a guarantee that two of the random items will be a specific element?

<div id = "choice1">
<div id = "choice2">
<div id = "choice3">
<div id = "choice4">
<div id = "choice5">
<div id = "choice6">
<div id = "choice7">
<div id = "choice8">
<div id = "choice9">
<div id = "choice10">
<div id = "choice11">
<div id = "choice12">
<div id = "choice13">
<div id = "choice14">
<div id = "choice15">
<div id = "choice16">
var elem = ["a","b","c","d"...];
var o1 = Math.floor(Math.random() * elem.length);
var obj1 = elem[o1];
document.getElementById("choice1").innerHTML = obj1;
...

1 Answers1

1

You can do this in plain JavaScript like this (shuffled derived from this post):

//"pre-select" required elements
var selectedElementIDs = ["choice1", "choice2"];

//define other possible elements
var otherPossibleElementIDs = ["choice3", "choice4", "choice5", "choice6", "choice7", "choice8", "choice9", "choice10", "choice11", "choice12", "choice13", "choice14", "choice15", "choice16"];

//"select" 3 more elements from the "possible elements" array
for (var i = 0; i < 3; i++) selectedElementIDs.push(otherPossibleElementIDs.splice(Math.floor(Math.random() * otherPossibleElementIDs.length), 1)[0]);

//shuffle your selections so the "pre-selected" required elements aren't always first
var j, x, i;
for (i = selectedElementIDs.length - 1; i > 0; i--) {
  j = Math.floor(Math.random() * (i + 1));
  x = selectedElementIDs[i];
  selectedElementIDs[i] = selectedElementIDs[j];
  selectedElementIDs[j] = x;
}

//log the result to the console to show it worked
console.log(selectedElementIDs);

Or, you can do this same thing in a more human-readable (and cryptographically secure) way with rando.js if you prefer:

//"pre-select" required elements
var selectedElementIDs = ["choice1", "choice2"];

//define other possible elements (and go ahead and shuffle them so "popping" from that array after this will produce a random next value)
var otherPossibleElementIDs = randoSequence(["choice3", "choice4", "choice5", "choice6", "choice7", "choice8", "choice9", "choice10", "choice11", "choice12", "choice13", "choice14", "choice15", "choice16"]);

//"select" 3 more elements from the "possible elements" array
for(var i = 0; i < 3; i++) selectedElementIDs.push(otherPossibleElementIDs.pop().value);

//shuffle your selections so the "pre-selected" required elements aren't always first
selectedElementIDs = randoSequence(selectedElementIDs);
for(var i = 0; i < selectedElementIDs.length; i++) selectedElementIDs[i] = selectedElementIDs[i].value;

//log the result to the console to show it worked
console.log(selectedElementIDs);
<script src="https://randojs.com/2.0.0.js"></script>
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15