Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following code may help. The calculateRandoms function calculates 5 different random numbers from the numbers array each time. The random number cannot be value of excludedNumber. You can change randomCount variable to modify the number of random numbers that calculateRandoms will generate.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var excludedNumber = 10; var randomCount = 5; var numbers = []; var randoms = []; var vPool=""; // initialize array, fill it from 0 to 99 for (var i = 0; i &lt; 100; i++) numbers.push(i + 1); $(document).ready(function() { $("#mybutton").click(function() { calculateRandoms(randomCount); }); }); function calculateRandoms(c) { randoms = []; for (var i = 0; i &lt; c; i++) { var rnd = excludedNumber; while (rnd == excludedNumber &amp;&amp; $.inArray(rnd, numbers)){ rnd = numbers[Math.floor(Math.random() * numbers.length)]; randoms.push(rnd); } } vPool=''; $.each(randoms, function(i, val) { vPool+=val + "&lt;br /&gt;"; $("#res").html(vPool); }); //alert(randoms); //$("#res").html(randoms); //console.log(randoms); }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;input type="button" id="mybutton" value="Generate" /&gt; &lt;div id="res"&gt;&lt;/div&gt;</code></pre> </div> </div> </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload