Set a minimum value for randomly generated numbers [duplicate]










0
















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










share|improve this question















marked as duplicate by Mohammad, trincot javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 '18 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 '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















0
















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










share|improve this question















marked as duplicate by Mohammad, trincot javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 '18 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 '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













0












0








0


1







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










share|improve this question

















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 '18 at 10:39









Mohammad

15.5k123461




15.5k123461










asked Nov 11 '18 at 10:11









BillBill

7017




7017




marked as duplicate by Mohammad, trincot javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 '18 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 javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 '18 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 '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





    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – Á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




1





developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Álvaro González
Nov 11 '18 at 10:16





developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– Á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





@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





@Bill Promlem fixed

– Mohammad
Nov 11 '18 at 10:30












1 Answer
1






active

oldest

votes


















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>








share|improve this answer

























  • please add the function to your post too, getRandomIntInclusive

    – hanshenrik
    Nov 11 '18 at 10:23







  • 1





    @hanshenrik For this question only need this part of function so i removed additional code

    – Mohammad
    Nov 11 '18 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 '18 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 '18 at 10:32






  • 1





    @Bill Check jsfiddle if number is below 50 color changed to red

    – Mohammad
    Nov 11 '18 at 10:36


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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>








share|improve this answer

























  • please add the function to your post too, getRandomIntInclusive

    – hanshenrik
    Nov 11 '18 at 10:23







  • 1





    @hanshenrik For this question only need this part of function so i removed additional code

    – Mohammad
    Nov 11 '18 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 '18 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 '18 at 10:32






  • 1





    @Bill Check jsfiddle if number is below 50 color changed to red

    – Mohammad
    Nov 11 '18 at 10:36
















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>








share|improve this answer

























  • please add the function to your post too, getRandomIntInclusive

    – hanshenrik
    Nov 11 '18 at 10:23







  • 1





    @hanshenrik For this question only need this part of function so i removed additional code

    – Mohammad
    Nov 11 '18 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 '18 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 '18 at 10:32






  • 1





    @Bill Check jsfiddle if number is below 50 color changed to red

    – Mohammad
    Nov 11 '18 at 10:36














1












1








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>








share|improve this answer















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>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 '18 at 10:29

























answered Nov 11 '18 at 10:21









MohammadMohammad

15.5k123461




15.5k123461












  • please add the function to your post too, getRandomIntInclusive

    – hanshenrik
    Nov 11 '18 at 10:23







  • 1





    @hanshenrik For this question only need this part of function so i removed additional code

    – Mohammad
    Nov 11 '18 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 '18 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 '18 at 10:32






  • 1





    @Bill Check jsfiddle if number is below 50 color changed to red

    – Mohammad
    Nov 11 '18 at 10:36


















  • please add the function to your post too, getRandomIntInclusive

    – hanshenrik
    Nov 11 '18 at 10:23







  • 1





    @hanshenrik For this question only need this part of function so i removed additional code

    – Mohammad
    Nov 11 '18 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 '18 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 '18 at 10:32






  • 1





    @Bill Check jsfiddle if number is below 50 color changed to red

    – Mohammad
    Nov 11 '18 at 10:36

















please add the function to your post too, getRandomIntInclusive

– hanshenrik
Nov 11 '18 at 10:23






please add the function to your post too, getRandomIntInclusive

– hanshenrik
Nov 11 '18 at 10:23





1




1





@hanshenrik For this question only need this part of function so i removed additional code

– Mohammad
Nov 11 '18 at 10:27





@hanshenrik For this question only need this part of function so i removed additional code

– Mohammad
Nov 11 '18 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 '18 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 '18 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 '18 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 '18 at 10:32




1




1





@Bill Check jsfiddle if number is below 50 color changed to red

– Mohammad
Nov 11 '18 at 10:36






@Bill Check jsfiddle if number is below 50 color changed to red

– Mohammad
Nov 11 '18 at 10:36




Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)