简体   繁体   中英

How do I get a previous value of a random generated numbers 1 to 10?

I have a random generator from 1 to 10 that produces non repeating values I am trying to get the previous value. So if current is 5 and then 8 Previous 8 and next is 9, etc

This is my code:

var randomNumbers = [];
var numRandoms = 11;
var myVar = setInterval(randomUnique1to10, 5000);

function randomUnique1to10() {
// refill the array if needed
if (!randomNumbers.length) {
    for (var i = 1; i < numRandoms; i++) {
        randomNumbers.push(i);
    }
}
var index = Math.floor(Math.random() * randomNumbers.length);
var val = randomNumbers[index];

 if (i === 1) { // i would become 0
    i = randomNumbers.length; // so put it at the other end of the array
}
i = i - 1; // decrease by one
previous = randomNumbers[i]; // give us back the item of where we are now


randomNumbers.splice(index, 1);

Thank you

This is totally what you're looking for. Hope it helps!.

 var randomNumbers = []; var numRandoms = 11; var myVar = setInterval(randomUnique1to10, 1000); 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 finalArray = shuffle([1, 2, 3, 4,5,6,7,8,9,10]); function randomUnique1to10() { // refill the array if needed if (!randomNumbers.length) { for (var i = 1, l = 11; i < l; i++) { } } // var finalArray = shuffle(randomNumbers); document.write(finalArray + "<br/>"); randomNumbers = finalArray; var index = Math.floor(Math.random() * randomNumbers.length); var val = randomNumbers[index]; if (i === 1) { // i would become 0 i = randomNumbers.length; // so put it at the other end of the array } x = index-1; // decrease by one current = randomNumbers[randomNumbers.length - 1]; // give us back the item of where we are now previous = randomNumbers[randomNumbers.length - 2]; if(previous === undefined) { previous = "n/a"; } randomNumbers.pop(); if(randomNumbers.length <= 0){ finalArray = shuffle([1, 2, 3, 4,5,6,7,8,9,10]); } document.write("Current >> " + current + " and previous = " +previous + "<br/>") } 

try this:

var randomNumbers = [];
var numRandoms = 11;
var myVar = setInterval(randomUnique1to10, 5000);
var x;
function randomUnique1to10() {
// refill the array if needed
if (!randomNumbers.length) {
    for (var i = 1; i < numRandoms; i++) {
        randomNumbers.push(i);
    }
}
var index = Math.floor(Math.random() * randomNumbers.length);
var val = randomNumbers[index];

 if (i === 1) { // i would become 0
    i = randomNumbers.length; // so put it at the other end of the array
}
x = index-1; // decrease by one
previous = randomNumbers[x]; // give us back the item of where we are now


var res = randomNumbers.splice(index, 1);

console.log("result: "+res);
console.log("prev  : "+previous);
console.log("Rand  : "+randomNumbers);
}

NOTE: this code have, one problem, if res = min(array) then prev will become unidentify

The question lack a bit of clarity. From the question what I understood is,

  • The values in the array are non repeating
  • You have a value at variable current and you want to find the previous value of the current which is stored in the array

And I will answer from what I understood. Using indexOf() gives you the index of current element from the array and you just need to subtract 1 from it to get the previous.

 var array = [1, 9, 2, 8, 3, 7, 4, 6, 5]; var current = 8; var currentIndex = array.indexOf(current); var previous = currentIndex !== 0 ? array[currentIndex-1] : 'N/A'; document.write('Previous Value: ', previous) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM