Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random number, which is not equal to the previous number

Tags:

javascript

I need to get random number, but it should not be equal to the previous number. Here is my piece of the code. But it doesn't work.

function getNumber(){   var min = 0;   var max = 4;   var i;   i = Math.floor(Math.random() * (max - min)) + min;   if (i=== i) {     i = Math.floor(Math.random() * (max - min)) + min;   }   return i; };  console.log(getNumber()); 
like image 253
NJV Avatar asked Oct 15 '16 07:10

NJV


People also ask

How can I generate a random number within a range but exclude some?

After generating the random number you've to put the "holes" back in the range. This can be achieved by incrementing the generated number as long as there are excluded numbers lower than or equal to the generated one. The lower exclude numbers are "holes" in the range before the generated number.

Why is 17 the most common random number?

Seventeen is: Described at MIT as 'the least random number', according to the Jargon File. This is supposedly because in a study where respondents were asked to choose a random number from 1 to 20, 17 was the most common choice. This study has been repeated a number of times.

Can math random returns same number?

random() returns same sequence of numbers [SOLVED] While messing around with Math. random() in the lesson, on the scratch pad, and in the Codecademy Labs, I found that it generates the exact same sequence of numbers.


1 Answers

This answer presents three attempts

  1. A simple version with a property of the function getNumber, last, which stores the last random value.

  2. A version which uses a closure over the min and max values with raising an exception if max is smaller than min.

  3. A version which combines the closure and the idea of keeping all random values and use it as it seems appropriate.


One

You could use a property of getNumber to store the last number and use a do ... while loop.

function getNumber() {      var min = 0,          max = 4,          random;        do {          random = Math.floor(Math.random() * (max - min)) + min;      } while (random === getNumber.last);      getNumber.last = random;      return random;  };    var i;  for (i = 0; i < 100; i++) {      console.log(getNumber());  }
.as-console-wrapper { max-height: 100% !important; top: 0; }

Two

Another proposal with a closure over the interval and the last random value.

function setRandomInterval(min, max) {      var last;      if (min >= max) {          throw 'Selected interval [' + min + ', ' + max + ') does not work for random numbers.';      }      return function () {          var random;          do {              random = Math.floor(Math.random() * (max - min)) + min;          } while (random === last);          last = random;          return random;      };  }    var i,      getRandom = setRandomInterval(0, 4);    for (i = 0; i < 100; i++) {      console.log(getRandom());  }    setRandomInterval(4, 4); // throw error
.as-console-wrapper { max-height: 100% !important; top: 0; }

Three

This proposal uses the idea to minimise the call of a new random number. It works with two variables, value for the continuing same random value and count for saving the count of the same value.

The function looks first if the saved count is given and if the value is not equal with the last value. If that happens, the saved value is returned and count is decremented.

Otherwise a new random numner is generated and checked as above (first proposal). If the number is equal to the last value, the count is incremented and it goes on with generating a new random value.

As result, almost all previous generated random values are used.

function setRandomInterval(min, max) {      var last,      // keeping the last random value          value,     // value which is repeated selected          count = 0, // count of repeated value          getR = function () { return Math.floor(Math.random() * (max - min)) + min; };        if (min >= max) {          throw 'Selected interval [' + min + ', ' + max + ') does not work for random numbers.';      }      return function () {          var random;          if (count && value !== last) {              --count;              return last = value;          }          random = getR();          while (random === last) {              value = random;              ++count;              random = getR();          }          return last = random;      };  }    var i,      getRandom = setRandomInterval(0, 4);    for (i = 0; i < 100; i++) {      console.log(getRandom());  }
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 86
Nina Scholz Avatar answered Oct 13 '22 19:10

Nina Scholz