0

I would like to add a random number generator to my site. The idea is to have a code that outputs a random number between a min and max value (i.e. 200-400) that updates in real time, without any page refresh. I came across this, but it doesn't really do I need to since the number is generated on click rather than dynamically (without any action on the user's part). Plus, if I set the range to 400 and min value to 230, the snippet outputs numbers that aren't even within this range (i.e. 580).

It would be even better to have the numbers display not so randomly (i.e. 340, then 348, then 367, then 330, 332, 330, 345, 357 etc). But it isn't mandatory.

Thank you for the help!

3
  • 1
    not so randomly - can you elaborate? Also, when and how often would the number be generated? You can use setInterval to execute the generator every x milliseconds Commented Jan 19, 2017 at 1:28
  • 1
    you can drop the php tag this is pure js Commented Jan 19, 2017 at 1:34
  • @MaxSindwani Well, for example, the number cannot suddenly drop from 350 to 245. They should look a bit more... sequential, so to speak. The number should gradually increase, then decrease a bit, then increase again... An interval of about 6 seconds should be fine. Commented Jan 19, 2017 at 10:53

2 Answers 2

2

randWithVariation() takes a min and max value as well as a maximum offset to its previous value and returns a function you can use.

function randWithVariation(min, max, variation) {
  var seed = (Math.floor(Math.random() * max) % (max - min)) + min; // attempts to keep the result within the bounds
  var min = min, max = max, variation = variation;
  var r = function() {
    var offset = Math.floor(Math.random() * variation);
    if (Math.random() < 0.5) offset *= -1; // chance that number will decrease
    seed += offset;
    if (seed < min) return max - seed; // also attempts to keep the result within the bounds
    if (seed > max) return min + (seed - max);
    else return seed;
  }
  return r;
}

var rand = randWithVariation(200, 400, 10);
document.getElementById('rnd').innerHTML = rand();
setInterval(() => {
  document.getElementById('rnd').innerHTML = rand();
}, 500);
<span id="rnd"></span>

Sign up to request clarification or add additional context in comments.

8 Comments

This one is working like a charm! I adjusted the interval to 7000 to better fit my needs. However, it takes quite a lot of time to show up on the page after the page has been loaded (a few seconds actually). Can I make it show up faster (like, immediately after page load)? Thanks a bunch!
I suspect it might be due to the interval set to 7000. I think it displays for the first time on the page exactly after 7 seconds, so I'd need a way to make it load faster than that, and immediately on page load.
setInterval waits for the duration of its delay before it does anything, so run document.getElementById('rnd').innerHTML = rand(); once outside of the setInterval block.
Could you please clarify a little or possibly update the code snippet to reflect the change? I'm a bit confused. Thank you!
setInterval is a cycle of wait then run, rather than run and wait. So it will wait the 7 seconds before it runs the code inside. I've updated the snippet to also run rand() once before the 7 second delay.
|
1

This can be an option: http://www.w3schools.com/code/tryit.asp?filename=FBVP9Q02RZFB

or

<!DOCTYPE html>
<html>
<body>
<p>Lots of random numbers</p>
<p id="demo"></p>

<script>
//returns a random number beetween min and max
function randMinMax(min , max) {
    return min + Math.floor((Math.random() * max));

}
function updateEveryXms(min , max, ms) {
    //start allredy with a random number instad of waiting ms
    document.getElementById("demo").innerHTML = randMinMax(min,max);
    //do it for ever and ever
    setInterval(
        function(){
            document.getElementById("demo").innerHTML = randMinMax(min,max);
        }, ms);

}

updateEveryXms(10 , 150, 1000);

</script>

</body>
</html>

1 Comment

should be fine now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.