I am working in Matter.js, and I am not able to display a number between 2 numbers. I want help with what should be the correct code and in which function to write it.
I have no experience in Matter.js but this should work as this is pure javascript.
var randomNum = Math.floor(Math.random() * maxNum) + minNum;
Related
I am working on the following code. How can I create random number between 150.570 and 150.720?
As you can see the integer-part (150) is always fixed and I just need to get random on fractional-part (between .570 to .720) only.
var gapLeft = Math.floor(Math.random() * 150.720) + 150.570 ;
console.log(gapLeft);
Here is your solution for this,
console.log(150.57+Math.random()*(0.72-0.57));
Hi, I would like to get your opinion about this example.
Considering me as a beginner of coding.
I would like to add a random number after the URL GIF-File-Name.
Ex.:
Turn "intro_animation.gif" into "intro_animation.gif?v=7463" The number at the end should be random each time the browser refreshes, so the animated GIF can start again from the beginning.
I've seen many similar examples on stackoverflow.com, but I dont know how to put the parts together
...
Here is my code, which obviously don't work.
I want it to keep it as simple a possible.
<script>
setTimeout(function () {
document.getElementById("intro").style.backgroundImage = "url('./anim/intro_animation.gif' + ?v= + (Math.random() * 1000); )";
}, 0);
</script>
<div id="intro" class="intro_animation_gif" style="background-image: url('./anim/intro_animation.gif');">
</div>
You are not too far off. Make sure you put double quotes around the parts that are strings and concatenate with pluses.
You might also want to round the random number to make sure it does not contain decimals.
Sample:
document.getElementById("intro").style.backgroundImage =
"url('./anim/intro_animation.gif?v=" + Math.round((Math.random() * 1000)) + "')";
I'm coding a price calculator in JS and I'm stuck with one formula:
number = (parseFloat(newnumber, 10) * parseFloat(1.536, 10)).toString(10);
I want to add 7.44 to the value of newnumber, before it is multiplied with 1.536
I've tried several things, but with no success.
Going to submit this as an answer, even though someone has put this up a comment while I was typing my answer.
number = ((+newnumber + 7.44) * 1.536).toString();
That should give you a string representation of the summed value.
Use parentheses to make the addition before the multiplication.
number = ((parseFloat(newnumber) + 7.44) * 1.536).toString();
Notes: parseFloat doesn't have a radix parameter. There is no reason to parse the number 1.536, that will only turn it to a string and then back to the same number again. The default for the radix parameter for toString is 10, so that isn't needed.
number = ((parseFloat(newnumber) + 7.44) * parseFloat(1.536)).toString();?
Just use parentheses to separate out the operations. Simple fix.
Working DEMO
Try the following code -
var newnumber = '1';
var number = ((parseFloat(newnumber) + 7.44) * parseFloat(1.536)).toString(10);
alert(number);
I have a formula that generates a random integer in a given range [x,y].
rand = Math.floor(x + Math.random()*(y-x+1));
And I would like the generated integer to have a higher chance of being close to the midrange.
Here is an interesting approach.
I am trying to adapt that solution to my problem (numbers skewed towards the midrange, not the extremities), but I am struggling with the formula.
beta = Math.sin(Math.random()*Math.PI)^2;
rand = Math.floor(x + beta*(y-x+1));
I do not understand how beta works. According to this graph wouldn't the numbers generated have a higher chance of being closer to 0.5? beta always returns the same number. Didn't I implement Math.random() properly? I swear javascript is messing with me right now.
You probably need a
beta = 4 * (rand - 0.5)^3 + 0.5
function or something with a similar shape
Graph
Distribution results: http://jsfiddle.net/4hBqz/
I have a little problem that I can't seem to get my head round...
I have figure that I need to find what the VAT element of it would be, I have the following jQuery which works but it doesn't fix it to 2 decimal places.
var ThreeMonthPriceFinal = "99.29";
var ThreeMonthPriceVAT = ThreeMonthPriceFinal * 0.2.toFixed(2);
alert(ThreeMonthPriceVAT);
It works out the VAT correctly but adds lots of recurring digits that i don't need... I can't round it up as with VAT you not really supposed to.
http://jsfiddle.net/Xg4Qs/
The Jfiddle shows £19.858000000000004 I need this to show 19.86, i've tried the following but it rounds it up to the whole amount not just 1p.
var ThreeMonthPriceVAT = Math.round(ThreeMonthPriceFinal * 0.2).toFixed(2);
Can anyone help me?
Magic of parentheses
var ThreeMonthPriceVAT = (ThreeMonthPriceFinal * 0.2).toFixed(2);