dimanche 16 août 2020

How can I generate random items with conditions?

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;
...



Aucun commentaire:

Enregistrer un commentaire