# how to prevent random method from giving more than two same numbers js site:stackoverflow.com

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()); }

**Here is what the above code is Doing:**

1. The getNumber function is defined.

2. The getNumber function is called 100 times.

3. Each time the getNumber function is called, it generates a random number between 0 and 4.

4. The getNumber function checks to see if the random number is the same as the last random number.

5. If the random number is the same as the last random number, the getNumber function generates a new random number.

6. If the random number is not the same as the last random number, the getNumber function returns the random number.

7. The getNumber function stores the last random number in a property called last.

8. The getNumber function returns the random number.