Set a minimum value for randomly generated numbers [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
Generate random number between two numbers in JavaScript
18 answers
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
javascript jquery random
marked as duplicate by Mohammad, trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
Generate random number between two numbers in JavaScript
18 answers
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
javascript jquery random
marked as duplicate by Mohammad, trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Álvaro González
Nov 11 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 at 10:27
@Bill Promlem fixed
– Mohammad
Nov 11 at 10:30
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Generate random number between two numbers in JavaScript
18 answers
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
javascript jquery random
This question already has an answer here:
Generate random number between two numbers in JavaScript
18 answers
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
This question already has an answer here:
Generate random number between two numbers in JavaScript
18 answers
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>
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>
javascript jquery random
javascript jquery random
edited Nov 11 at 10:39
Mohammad
13.6k93158
13.6k93158
asked Nov 11 at 10:11
Bill
7017
7017
marked as duplicate by Mohammad, trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mohammad, trincot
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 10:31
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Álvaro González
Nov 11 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 at 10:27
@Bill Promlem fixed
– Mohammad
Nov 11 at 10:30
add a comment |
1
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Álvaro González
Nov 11 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 at 10:27
@Bill Promlem fixed
– Mohammad
Nov 11 at 10:30
1
1
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Álvaro González
Nov 11 at 10:16
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Álvaro González
Nov 11 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 at 10:27
@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 at 10:27
@Bill Promlem fixed
– Mohammad
Nov 11 at 10:30
@Bill Promlem fixed
– Mohammad
Nov 11 at 10:30
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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>
please add the function to your post too,getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
1
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
1
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
1
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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>
please add the function to your post too,getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
1
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
1
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
1
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
|
show 1 more comment
up vote
1
down vote
accepted
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>
please add the function to your post too,getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
1
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
1
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
1
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
|
show 1 more comment
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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>
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>
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>
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>
edited Nov 11 at 10:29
answered Nov 11 at 10:21
Mohammad
13.6k93158
13.6k93158
please add the function to your post too,getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
1
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
1
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
1
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
|
show 1 more comment
please add the function to your post too,getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
1
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
1
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
1
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
please add the function to your post too,
getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
please add the function to your post too,
getRandomIntInclusive
– hanshenrik
Nov 11 at 10:23
1
1
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@hanshenrik For this question only need this part of function so i removed additional code
– Mohammad
Nov 11 at 10:27
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
@Mohammad I can see that the most generated numbers are above 50, but I am still getting numbers under 50 too.
– Bill
Nov 11 at 10:31
1
1
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
@Bill that means you did not do it as in this answer. Run the snippet -- there is never a value below 50.
– trincot
Nov 11 at 10:32
1
1
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
@Bill Check jsfiddle if number is below 50 color changed to red
– Mohammad
Nov 11 at 10:36
|
show 1 more comment
1
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Álvaro González
Nov 11 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 at 10:27
@Bill Promlem fixed
– Mohammad
Nov 11 at 10:30