How to draw lots figures always different like in the lottery? - javascript
I would like to ask about one tiny riddle. I want to draw lots within in this example 15 figures that should always be different like in the lottery. I have already checked first condition. If for example 1 is equal 1 - please draw lots again, but there is possibility that next sorting will show again number 1. I would like to avoid this some way, to exclude it. There cannot be drawn lots figures the same. Some ideas? :-)
var figureShowed = document.querySelector("div");
var button = document.getElementById("random-figure");
var allFigures = [];
button.addEventListener("click", drawAmount);
function drawAmount() {
for (i = 0; i <= 5; i++) {
var random = Math.floor(Math.random() * 15 + 1);
if (allFigures[0] === allFigures[1, 2, 3, 4, 5]) {
allFigures[0] = random;
} else {
allFigures[i] = random;
}
}
figureShowed.textContent = allFigures.join(" | ");
}
You can use includes method to check if random number is already in allFigures array. If it is, then you draw lots again. Check the code:
var figureShowed = document.querySelector("div");
var button = document.getElementById("random-figure");
var allFigures = [];
button.addEventListener("click", drawAmount);
function drawAmount() {
allFigures = [];
for (i = 0; i <= 5; i++) {
let random;
do {
random = Math.floor(Math.random() * 15 + 1);
} while (allFigures.includes(random));
allFigures.push(random);
}
figureShowed.textContent = allFigures.join(" | ");
}
<button id="random-figure">random figure</button>
<div></div>
Add each random number to an array, alreadyDrawn, and compare subsequent randoms to the numbers in that array. If you have a match, discard and re-random until you don't have a match.
This is probably a naive or non-optimal solution to your problem, but it should work.
Related
Google Script working as 2 separate scripts but not inside the same function
Basically I have a script that is in 4 blocks: 1. Copies within a range each row provided it meets a criteria 2. Removes all empty rows 3. Sets all numbers as percentage 4. Applies conditional cell formatting to one of the columns The 4th part is the one that is causing me issues. The script runs without any error message AND block 4 works perfectly fine if it's in another script alone with the same variables defined but as soon as it is inside the same function as the others it simply doesn't run without any error message of any kind. Tried changing the name of the variables to single use ones to ensure it wasn't because one of the "var" was modified above it, removing the "else if" to keep only an "if" in the loop, moving it around to other parts of the script but if the block 1 is in the script then block 4 won't apply (will apply if it is only with 2 & 3. 2 & 3 which follow the same structure work well with 1. Does any one have any clue what's wrong with my script ? :) Each block is commented with what it does function copy() { //Set variables & criterion to choose which rows to copy var s = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1bEiLWsbFszcsz0tlQudMBgTk5uviyv_wDx7fFa8txFM/edit'); var ssSource = s.getSheetByName('Variations'); var ssDest = s.getSheetByName('Email'); var lastRowSource = ssSource.getLastRow(); var lastRowDest = ssDest.getLastRow(); var lastColSource = ssSource.getLastColumn() var criteria = 0; var titles = ssSource.getRange(1,1,1, lastColSource).getValues() //Copies the range ssDest.getRange(1,1,1, lastColSource).setValues(titles) for (var i = 2; i < lastRowSource; i++ ) { var test = ssSource.getRange(i ,1); Logger.log(test.getValue()+ ' <? ' + criteria); if (ssSource.getRange(i ,6).getValue() > criteria) { ssSource.getRange(i ,1,1,ssSource.getLastColumn()).copyTo(ssDest.getRange(i ,1,1,ssSource.getLastColumn()), {contentsOnly:true}); // copy/paste content only } } //Removes empty rows var data = ssDest.getDataRange().getValues(); var targetData = new Array(); for(n=0;n<data.length;++n){ if(data[n].join().replace(/,/g,'')!=''){ targetData.push(data[n])}; Logger.log(data[n].join().replace(/,/g,'')) } ssDest.getDataRange().clear(); ssDest.getRange(1,1,targetData.length,targetData[0].length).setValues(targetData); //Formats numbers as percentages var rangePercent = ssDest.getRange(1,1,ssDest.getLastRow(),ssDest.getLastColumn()); var rowsPercent = rangePercent.getNumRows(); var colsPercent = rangePercent.getNumColumns(); for(var rowPercent = 1; rowPercent <= rowsPercent; rowPercent++) { for(var colPercent = 1; colPercent <= colsPercent; colPercent++) { var cellPercent = rangePercent.getCell(rowPercent, colPercent); var valuePercent = cellPercent.getValue(); if(typeof(valuePercent) == 'number') { cellPercent.setNumberFormat("##.#%"); } } } //Adds conditional background colours for (var z = 2; z < lastRowDest+1;z++) { var avgCpc = 4; var rangeColour = ssDest.getRange(z,avgCpc); var dataColour = rangeColour.getValue() if (dataColour < 0) { ssDest.getRange(z,avgCpc).setBackground('#d9ead3') } else if (dataColour > 0) { ssDest.getRange(z,avgCpc).setBackground('#f4cccc') } } //Centers Values }
The problem you're having is your code has performance issues because you're calling too many times methods such as getRange() and getValue() inside various loops, therefore Apps Script can't keep up with all those calls. Please check Best Practices. Having said that, I modified your code in order to make it more efficient. Besides your copy function, I added two more functions to make the code more readable. function copy As before this function sets the variables, but now it calls two other functions, which are setPositiveCostValues and formatCells function copy() { //Set variables & criterion to choose which rows to copy var ss = SpreadsheetApp.openByUrl('your-url'); var ssSource = ss.getSheetByName('Variations'); var ssDest = ss.getSheetByName('Email'); // set the title var titles = ssSource.getRange(1,1,1, ssSource.getLastColumn()).getValues(); ssDest.getRange(1,1,1, ssSource.getLastColumn()).setValues(titles); // get the positive values you want from the cost col var positiveValues = setPositiveCostValues(ssSource, ssDest, ssSource.getLastRow()); // fomrat the cells you want as percentage and set the color formatCells(ssDest, positiveValues); } function setPositiveCostValues This will take the values where the cost is positive and it will get rip off of the cells with empty values and "n/a" values. function setPositiveCostValues(ssSource,ssDest, lastRowSource){ var postiveCost = ssSource.getRange(2, 1, lastRowSource, 6).getValues(); // this loop will clean the empty elements and the ones that only have n/a for (var i = postiveCost.length - 1; i >= 0; i--) { if (postiveCost[i][0]) { postiveCost.splice(i + 1, postiveCost.length - (i + 1)); postiveCost = postiveCost.filter(function(el){ return el != 'n/a'}) break; } } return postiveCost; } function formatCells This will format the cells in the cost col as a percentage and will set the right color in your avgCpc col. function formatCells(ssDest, postiveCost){ var avgCpc = 4, cost = 6, row = 2, criteria = 0; // iterate over the array and depending on the criteria format the cells postiveCost.forEach(function(el){ if(el[cost - 1] > criteria){ var ssDestRange = ssDest.getRange(row, 1, 1, cost); ssDestRange.setValues([el]); ssDestRange.getCell(1, cost).setNumberFormat("##.#%"); // set the color depending on the avgCpc value condition if(el[avgCpc - 1] < criteria) ssDest.getRange(row, avgCpc).setBackground('#d9ead3'); else ssDest.getRange(row, avgCpc).setBackground('#f4cccc'); row++; } }); } Code Your whole code now it will look like this: function copy() { //Set variables & criterion to choose which rows to copy var ss = SpreadsheetApp.openByUrl('your-url'); var ssSource = ss.getSheetByName('Variations'); var ssDest = ss.getSheetByName('Email'); // set the title var titles = ssSource.getRange(1,1,1, ssSource.getLastColumn()).getValues(); ssDest.getRange(1,1,1, ssSource.getLastColumn()).setValues(titles); // get the positive values you want from the cost col var positiveValues = setPositiveCostValues(ssSource, ssDest, ssSource.getLastRow()); // fomrat the cells you want as percentage and set the color formatCells(ssDest, positiveValues); } function setPositiveCostValues(ssSource,ssDest, lastRowSource){ var postiveCost = ssSource.getRange(2, 1, lastRowSource, 6).getValues(); // this loop will clean the empty elements and the ones that only have n/a for (var i = postiveCost.length - 1; i >= 0; i--) { if (postiveCost[i][0]) { postiveCost.splice(i + 1, postiveCost.length - (i + 1)); postiveCost = postiveCost.filter(function(el){ return el != 'n/a'}) break; } } return postiveCost; } function formatCells(ssDest, postiveCost){ var avgCpc = 4, cost = 6, row = 2, criteria = 0; // iterate over the array and depending on the criteria format the cells postiveCost.forEach(function(el){ if(el[cost - 1] > criteria){ var ssDestRange = ssDest.getRange(row, 1, 1, cost); ssDestRange.setValues([el]); ssDestRange.getCell(1, cost).setNumberFormat("##.#%"); // set the color depending on the avgCpc value condition if(el[avgCpc - 1] < criteria) ssDest.getRange(row, avgCpc).setBackground('#d9ead3'); else ssDest.getRange(row, avgCpc).setBackground('#f4cccc'); row++; } }); }
How can I make the unique random numbers from this code appear on my page?
Someone showed me the following code which generates 3 random numbers between 1 and 10: var limit = 10, amount = 3, lower_bound = 1, upper_bound = 10, unique_random_numbers = []; if (amount > limit) limit = amount; //Infinite loop if you want more unique //Natural numbers than exist in a // given range while (unique_random_numbers.length < limit) { var random_number = Math.floor(Math.random()*(upper_bound - lower_bound) + lower_bound); if (unique_random_numbers.indexOf(random_number) == -1) { // Yay! new random number unique_random_numbers.push( random_number ); } } // How could I make these numbers appear in place of elements with a corresponding class? The code below is clearly wrong, but hopefully it illustrates what I'm trying to achieve: <script type='text/javascript'> var random_number1 = random_number1(); $('.random_number1').html(random_number1); var random_number2 = random_number2(); $('.random_number2').html(random_number2); </script> <span class="random_number1"></span> <span class = "random_number2"></span>
assuming you had a div with id of numbers and the array unique_random_numbers, you'd populate it this way, assuming you have jquery reference: for ( i = 0; i < unique_random_numbers.length; i++) { $("#Numbers").html($("#Numbers").html() +"<span>" + unique_random_numbers[i] + "</span><br/>"); }
Efficiently find every combination of assigning smaller bins to larger bins
Let's say I have 7 small bins, each bin has the following number of marbles in it: var smallBins = [1, 5, 10, 20, 30, 4, 10]; I assign these small bins to 2 large bins, each with the following maximum capacity: var largeBins = [40, 50]; I want to find EVERY combination of how the small bins can be distributed across the big bins without exceeding capacity (eg put small bins #4,#5 in large bin #2, the rest in #1). Constraints: Each small bin must be assigned to a large bin. A large bin can be left empty This problem is easy to solve in O(n^m) O(2^n) time (see below): just try every combination and if capacity is not exceeded, save the solution. I'd like something faster, that can handle a variable number of bins. What obscure graph theory algorithm can I use to reduce the search space? //Brute force var smallBins = [1, 5, 10, 20, 30, 4, 10]; var largeBins = [40, 50]; function getLegitCombos(smallBins, largeBins) { var legitCombos = []; var assignmentArr = new Uint32Array(smallBins.length); var i = smallBins.length-1; while (true) { var isValid = validate(assignmentArr, smallBins, largeBins); if (isValid) legitCombos.push(new Uint32Array(assignmentArr)); var allDone = increment(assignmentArr, largeBins.length,i); if (allDone === true) break; } return legitCombos; } function increment(assignmentArr, max, i) { while (i >= 0) { if (++assignmentArr[i] >= max) { assignmentArr[i] = 0; i--; } else { return i; } } return true; } function validate(assignmentArr, smallBins, largeBins) { var totals = new Uint32Array(largeBins.length); for (var i = 0; i < smallBins.length; i++) { var assignedBin = assignmentArr[i]; totals[assignedBin] += smallBins[i]; if (totals[assignedBin] > largeBins[assignedBin]) { return false; } } return true; } getLegitCombos(smallBins, largeBins);
Here's my cumbersome recursive attempt to avoid duplicates and exit early from too large sums. The function assumes duplicate elements as well as bin sizes are presented grouped and counted in the input. Rather than place each element in each bin, each element is placed in only one of duplicate bins; and each element with duplicates is partitioned distinctly. For example, in my results, the combination, [[[1,10,20]],[[4,5,10,30]]] appears once; while in the SAS example in Leo's answer, twice: once as IN[1]={1,3,4} IN[2]={2,5,6,7} and again as IN[1]={1,4,7} IN[2]={2,3,5,6}. Can't vouch for efficiency or smooth-running, however, as it is hardly tested. Perhaps stacking the calls rather than recursing could weigh lighter on the browser. JavaScript code: function f (as,bs){ // i is the current element index, c its count; // l is the lower-bound index of partitioned element function _f(i,c,l,sums,res){ for (var j=l; j<sums.length; j++){ // find next available duplicate bin to place the element in var k=0; while (sums[j][k] + as[i][0] > bs[j][0]){ k++; } // a place for the element was found if (sums[j][k] !== undefined){ var temp = JSON.stringify(sums), _sums = JSON.parse(temp); _sums[j][k] += as[i][0]; temp = JSON.stringify(res); var _res = JSON.parse(temp); _res[j][k].push(as[i][0]); // all elements were placed if (i == as.length - 1 && c == 1){ result.push(_res); return; // duplicate elements were partitioned, continue to next element } else if (c == 1){ _f(i + 1,as[i + 1][1],0,_sums,_res); // otherwise, continue partitioning the same element with duplicates } else { _f(i,c - 1,j,_sums,_res); } } } } // initiate variables for the recursion var sums = [], res = [] result = []; for (var i=0; i<bs.length; i++){ sums[i] = []; res[i] = []; for (var j=0; j<bs[i][1]; j++){ sums[i][j] = 0; res[i][j] = []; } } _f(0,as[0][1],0,sums,res); return result; } Output: console.log(JSON.stringify(f([[1,1],[4,1],[5,1],[10,2],[20,1],[30,1]], [[40,1],[50,1]]))); /* [[[[1,4,5,10,10]],[[20,30]]],[[[1,4,5,10,20]],[[10,30]]],[[[1,4,5,20]],[[10,10,30]]] ,[[[1,4,5,30]],[[10,10,20]]],[[[1,4,10,20]],[[5,10,30]]],[[[1,4,30]],[[5,10,10,20]]] ,[[[1,5,10,20]],[[4,10,30]]],[[[1,5,30]],[[4,10,10,20]]],[[[1,10,20]],[[4,5,10,30]]] ,[[[1,30]],[[4,5,10,10,20]]],[[[4,5,10,20]],[[1,10,30]]],[[[4,5,30]],[[1,10,10,20]]] ,[[[4,10,20]],[[1,5,10,30]]],[[[4,30]],[[1,5,10,10,20]]],[[[5,10,20]],[[1,4,10,30]]] ,[[[5,30]],[[1,4,10,10,20]]],[[[10,10,20]],[[1,4,5,30]]],[[[10,20]],[[1,4,5,10,30]]] ,[[[10,30]],[[1,4,5,10,20]]],[[[30]],[[1,4,5,10,10,20]]]] */ console.log(JSON.stringify(f([[1,1],[4,1],[5,1],[10,2],[20,1],[30,1]], [[20,2],[50,1]]))); /* [[[[1,4,5,10],[10]],[[20,30]]],[[[1,4,5,10],[20]],[[10,30]]],[[[1,4,5],[20]],[[10,10,30]]] ,[[[1,4,10],[20]],[[5,10,30]]],[[[1,5,10],[20]],[[4,10,30]]],[[[1,10],[20]],[[4,5,10,30]]] ,[[[4,5,10],[20]],[[1,10,30]]],[[[4,10],[20]],[[1,5,10,30]]],[[[5,10],[20]],[[1,4,10,30]]] ,[[[10,10],[20]],[[1,4,5,30]]],[[[10],[20]],[[1,4,5,10,30]]]] */ Here's a second, simpler version that only attempts to terminate the thread when an element cannot be placed: function f (as,bs){ var stack = [], sums = [], res = [] result = []; for (var i=0; i<bs.length; i++){ res[i] = []; sums[i] = 0; } stack.push([0,sums,res]); while (stack[0] !== undefined){ var params = stack.pop(), i = params[0], sums = params[1], res = params[2]; for (var j=0; j<sums.length; j++){ if (sums[j] + as[i] <= bs[j]){ var _sums = sums.slice(); _sums[j] += as[i]; var temp = JSON.stringify(res); var _res = JSON.parse(temp); _res[j].push(i); if (i == as.length - 1){ result.push(_res); } else { stack.push([i + 1,_sums,_res]); } } } } return result; } Output: var r = f([1,5,10,20,30,4,10,3,4,5,1,1,2],[40,50,30]); console.log(r.length) console.log(JSON.stringify(f([1,4,5,10,10,20,30], [40,50]))); 162137 [[[30],[1,4,5,10,10,20]],[[10,30],[1,4,5,10,20]],[[10,20],[1,4,5,10,30]] ,[[10,30],[1,4,5,10,20]],[[10,20],[1,4,5,10,30]],[[10,10,20],[1,4,5,30]] ,[[5,30],[1,4,10,10,20]],[[5,10,20],[1,4,10,30]],[[5,10,20],[1,4,10,30]] ,[[4,30],[1,5,10,10,20]],[[4,10,20],[1,5,10,30]],[[4,10,20],[1,5,10,30]] ,[[4,5,30],[1,10,10,20]],[[4,5,10,20],[1,10,30]],[[4,5,10,20],[1,10,30]] ,[[1,30],[4,5,10,10,20]],[[1,10,20],[4,5,10,30]],[[1,10,20],[4,5,10,30]] ,[[1,5,30],[4,10,10,20]],[[1,5,10,20],[4,10,30]],[[1,5,10,20],[4,10,30]] ,[[1,4,30],[5,10,10,20]],[[1,4,10,20],[5,10,30]],[[1,4,10,20],[5,10,30]] ,[[1,4,5,30],[10,10,20]],[[1,4,5,20],[10,10,30]],[[1,4,5,10,20],[10,30]] ,[[1,4,5,10,20],[10,30]],[[1,4,5,10,10],[20,30]]]
This problem is seen often enough that most Constraint Logic Programming systems include a predicate to model it explicitly. In OPTMODEL and CLP, we call it pack: proc optmodel; set SMALL init 1 .. 7, LARGE init 1 .. 2; num size {SMALL} init [1 5 10 20 30 4 10]; num capacity{LARGE} init [40 50]; var WhichBin {i in SMALL} integer >= 1 <= card(LARGE); var SpaceUsed{i in LARGE} integer >= 0 <= capacity[i]; con pack( WhichBin, size, SpaceUsed ); solve with clp / findall; num soli; set IN{li in LARGE} = {si in SMALL: WhichBin[si].sol[soli] = li}; do soli = 1 .. _nsol_; put IN[*]=; end; quit; This code produces all the solutions in 0.06 seconds on my laptop: IN[1]={1,2,3,4,6} IN[2]={5,7} IN[1]={1,2,3,4} IN[2]={5,6,7} IN[1]={1,2,3,6,7} IN[2]={4,5} IN[1]={1,2,5,6} IN[2]={3,4,7} IN[1]={1,2,5} IN[2]={3,4,6,7} IN[1]={1,2,4,6,7} IN[2]={3,5} IN[1]={1,2,4,7} IN[2]={3,5,6} IN[1]={1,2,4,6} IN[2]={3,5,7} IN[1]={1,3,4,6} IN[2]={2,5,7} IN[1]={1,3,4} IN[2]={2,5,6,7} IN[1]={1,5,6} IN[2]={2,3,4,7} IN[1]={1,5} IN[2]={2,3,4,6,7} IN[1]={1,4,6,7} IN[2]={2,3,5} IN[1]={1,4,7} IN[2]={2,3,5,6} IN[1]={2,3,4,6} IN[2]={1,5,7} IN[1]={2,3,4} IN[2]={1,5,6,7} IN[1]={2,5,6} IN[2]={1,3,4,7} IN[1]={2,5} IN[2]={1,3,4,6,7} IN[1]={2,4,6,7} IN[2]={1,3,5} IN[1]={2,4,7} IN[2]={1,3,5,6} IN[1]={3,5} IN[2]={1,2,4,6,7} IN[1]={3,4,7} IN[2]={1,2,5,6} IN[1]={3,4,6} IN[2]={1,2,5,7} IN[1]={3,4} IN[2]={1,2,5,6,7} IN[1]={5,7} IN[2]={1,2,3,4,6} IN[1]={5,6} IN[2]={1,2,3,4,7} IN[1]={5} IN[2]={1,2,3,4,6,7} IN[1]={4,6,7} IN[2]={1,2,3,5} IN[1]={4,7} IN[2]={1,2,3,5,6} Just change the first 3 lines to solve for other instances. However, as others have pointed out, this problem is NP-Hard. So it can switch from very fast to very slow suddenly. You could also solve the version where not every small item needs to be assigned to a large bin by creating a dummy large bin with enough capacity to fit the entire collection of small items. As you can see from the "Details" section in the manual, the algorithms that solve practical problems quickly are not simple, and their implementation details make a big difference. I am unaware of any CLP libraries written in Javascript. Your best bet may be to wrap CLP in a web service and invoke that service from your Javascript code.
JavaScript: how to stop a random number from appearing twice
How can you, in using a random number generator, stop a number from appearing if it has already appeared once? Here is the current code: var random = Math.ceil(Math.random() * 24); But the numbers appear more than once.
You can use an array of possible values ( I think in your case it will be 24 ) : var values = []; for (var i = 1; i <= 24; ++i){ values.push(i); } When you want to pick a random number you just do: var random = values.splice(Math.random()*values.length,1)[0];
If you know how many numbers you want then it's easy, first create an array: var arr = []; for (var i = 0; i <= 24; i++) arr.push(i); Then you can shuffle it with this little function: function shuffle(arr) { return arr.map(function(val, i) { return [Math.random(), i]; }).sort().map(function(val) { return val[1]; }); } And use it like so: console.log(shuffle(arr)); //=> [2,10,15..] random array from 0 to 24
You can always use an hashtable and before using the new number, check if it is in there or not. That would work for bigger numbers. Now for 24, you can always shuffle an array.
You could put the numbers you generate in an array and then check against that. If the value is found, try again: var RandomNumber = (function() { // Store used numbers here. var _used = []; return { get: function() { var random = Math.ceil(Math.random() * 24); for(var i = 0; i < _used.length; i++) { if(_used[i] === random) { // Do something here to prevent stack overflow occuring. // For example, you could just reset the _used list when you // hit a length of 24, or return something representing that. return this.get(); } } _used.push(random); return random; } } })(); You can test being able to get all unique values like so: for(var i = 0; i < 24; i++) { console.log( RandomNumber.get() ); } The only issue currently is that you will get a stack overflow error if you try and get a random number more times than the amount of possible numbers you can get (in this case 24).
math random number without repeating a previous number
Can't seem to find an answer to this, say I have this: setInterval(function() { m = Math.floor(Math.random()*7); $('.foo:nth-of-type('+m+')').fadeIn(300); }, 300); How do I make it so that random number doesn't repeat itself. For example if the random number is 2, I don't want 2 to come out again.
There are a number of ways you could achieve this. Solution A: If the range of numbers isn't large (let's say less than 10), you could just keep track of the numbers you've already generated. Then if you generate a duplicate, discard it and generate another number. Solution B: Pre-generate the random numbers, store them into an array and then go through the array. You could accomplish this by taking the numbers 1,2,...,n and then shuffle them. shuffle = function(o) { for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; }; var randorder = shuffle([0,1,2,3,4,5,6]); var index = 0; setInterval(function() { $('.foo:nth-of-type('+(randorder[index++])+')').fadeIn(300); }, 300); Solution C: Keep track of the numbers available in an array. Randomly pick a number. Remove number from said array. var randnums = [0,1,2,3,4,5,6]; setInterval(function() { var m = Math.floor(Math.random()*randnums.length); $('.foo:nth-of-type('+(randnums[m])+')').fadeIn(300); randnums = randnums.splice(m,1); }, 300);
You seem to want a non-repeating random number from 0 to 6, so similar to tskuzzy's answer: var getRand = (function() { var nums = [0,1,2,3,4,5,6]; var current = []; function rand(n) { return (Math.random() * n)|0; } return function() { if (!current.length) current = nums.slice(); return current.splice(rand(current.length), 1); } }()); It will return the numbers 0 to 6 in random order. When each has been drawn once, it will start again.
could you try that, setInterval(function() { m = Math.floor(Math.random()*7); $('.foo:nth-of-type(' + m + ')').fadeIn(300); }, 300);
I like Neal's answer although this is begging for some recursion. Here it is in java, you'll still get the general idea. Note that you'll hit an infinite loop if you pull out more numbers than MAX, I could have fixed that but left it as is for clarity. edit: saw neal added a while loop so that works great. public class RandCheck { private List<Integer> numbers; private Random rand; private int MAX = 100; public RandCheck(){ numbers = new ArrayList<Integer>(); rand = new Random(); } public int getRandomNum(){ return getRandomNumRecursive(getRand()); } private int getRandomNumRecursive(int num){ if(numbers.contains(num)){ return getRandomNumRecursive(getRand()); } else { return num; } } private int getRand(){ return rand.nextInt(MAX); } public static void main(String[] args){ RandCheck randCheck = new RandCheck(); for(int i = 0; i < 100; i++){ System.out.println(randCheck.getRandomNum()); } } }
Generally my approach is to make an array containing all of the possible values and to: Pick a random number <= the size of the array Remove the chosen element from the array Repeat steps 1-2 until the array is empty The resulting set of numbers will contain all of your indices without repetition. Even better, maybe something like this: var numArray = [0,1,2,3,4,5,6]; numArray.shuffle(); Then just go through the items because shuffle will have randomized them and pop them off one at a time.
Here's a simple fix, if a little rudimentary: if(nextNum == lastNum){ if (nextNum == 0){nextNum = 7;} else {nextNum = nextNum-1;} } If the next number is the same as the last simply minus 1 unless the number is 0 (zero) and set it to any other number within your set (I chose 7, the highest index). I used this method within the cycle function because the only stipulation on selecting a number was that is musn't be the same as the last one. Not the most elegant or technically gifted solution, but it works :)
Use sets. They were introduced to the specification in ES6. A set is a data structure that represents a collection of unique values, so it cannot include any duplicate values. I needed 6 random, non-repeatable numbers ranging from 1-49. I started with creating a longer set with around 30 digits (if the values repeat the set will have less elements), converted the set to array and then sliced it's first 6 elements. Easy peasy. Set.length is by default undefined and it's useless that's why it's easier to convert it to an array if you need specific length. let randomSet = new Set(); for (let index = 0; index < 30; index++) { randomSet.add(Math.floor(Math.random() * 49) + 1) }; let randomSetToArray = Array.from(randomSet).slice(0,6); console.log(randomSet); console.log(randomSetToArray);
An easy way to generate a list of different numbers, no matter the size or number: function randomNumber(max) { return Math.floor(Math.random() * max + 1); } const list = [] while(list.length < 10 ){ let nbr = randomNumber(500) if(!list.find(el => el === nbr)) list.push(nbr) } console.log("list",list)
I would like to add-- var RecordKeeper = {}; SRandom = function () { currTimeStamp = new Date().getTime(); if (RecordKeeper.hasOwnProperty(currTimeStamp)) { RecordKeeper[currTimeStamp] = RecordKeeper[currTimeStamp] + 1; return currTimeStamp.toString() + RecordKeeper[currTimeStamp]; } else { RecordKeeper[currTimeStamp] = 1; return currTimeStamp.toString() + RecordKeeper[currTimeStamp]; } } This uses timestamp (every millisecond) to always generate a unique number.
you can do this. Have a public array of keys that you have used and check against them with this function: function in_array(needle, haystack) { for(var key in haystack) { if(needle === haystack[key]) { return true; } } return false; } (function from: javascript function inArray) So what you can do is: var done = []; setInterval(function() { var m = null; while(m == null || in_array(m, done)){ m = Math.floor(Math.random()*7); } done.push(m); $('.foo:nth-of-type('+m+')').fadeIn(300); }, 300); This code will get stuck after getting all seven numbers so you need to make sure it exists after it fins them all.