1

I am using this function to generate and alternate random values inside a div

var change_price = $('#myprice');
animationTimer = setInterval(function() {
  change_price.text( ''+   Math.floor(Math.random() * 100) );      
}, 1000);
#myprice {
  padding: 20px;
  font-size:24px;
  color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myprice">50</div>

What I want to do is to control the min value too. For example, to have random generated values from 50 to 100. Not starting from zero(0) as other posts do

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Bill
  • 162
  • 1
  • 9
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_number_between_two_values – Álvaro González Nov 11 '18 at 10:16
  • @Mohammad I want to control the minimum value. The answer you posted is about to generate random number too, but with no minimum limit. values there starting from 0 – Bill Nov 11 '18 at 10:27
  • @Bill Promlem fixed – Mohammad Nov 11 '18 at 10:30

1 Answers1

1

As mentioned in mozilla developer you can generate random number between max and min as shown in bottom

Math.floor(Math.random() * (max - min + 1)) + min;

So your code should changed to

var change_price = $('#myprice');
animationTimer = setInterval(function() {
  change_price.text(Math.floor(Math.random() * (100-50+1)) + 50);
}, 100);
#myprice {
  padding: 20px;
  font-size:24px;
  color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myprice">50</div>
Mohammad
  • 21,175
  • 15
  • 55
  • 84