Creating random number on fractional part between two numbers - javascript

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));

Related

How to display a random number between 2 numbers in Matter.js?

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;

list fo random numbers separated by commas

Okay so I am making a mario inspired game with randomly generating terrain, It is all working fine however the array of random numbers that randomises the terrain must be the same each time so that the user can enter a seed which is then merged with the larger list to provide a set of random numbers based off of the seed however I cannot think of any way to make this array the same each time without writing it out, and even then making an array of 1000 numbers will be timely. Can anyone suggest a fast way (number generators online dont format it in one single line of numbers separated by numbers so cannot use them)
or could someone provide me with a list that is on a single line separated by numbers that i can easily copy and paste into an array thanks! :)
The following code in Javascript will generate 1000 random numbers separated by commas.
var string = "";
var numberOfRandomNumbers = 1000;
for (var i = 0; i < numberOfRandomNumbers; i++) {
var randomNumber = Math.floor((Math.random() * 1000) + 1); //Will generate random number between 1 and 1000
string += randomNumber+",";
}
console.log(string.substring(0, string.length - 1)); //Print string to console and remove last comma

Javascript coding a calculation

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);

Getting a random background color in javascript

I am new to java-script . I need to get a random background color whenever i call a particular function.
I found the following code on the web but i don't quite understand how it works.
Code:
function getRandomColor () {
var hex = Math.floor(Math.random() * 0xFFFFFF);
return "#" + ("000000" + hex.toString(16)).substr(-6);
}
How is the above code working.I understand how Math.random() works but what does hex.toString(16)).substr(-6) basically signify?
Can some one please clarify it to me how the above code works.
function getRandomColor () {
var hex = Math.floor(Math.random() * 0xFFFFFF);
return "#" + ("000000" + hex.toString(16)).substr(-6);
}
hex.toString(16) converts hex into string number representation in base 16.
Syntax:
number.toString(radix)
radix: Base to use for representing a numeric value. Must be an integer between 2 and 36.
2 - The number will show as a binary value
8 - The number will show as an octal value
16 - The number will show as an hexadecimal value
substr(-6) just takes the last 6 characters, which cuts off the "000000" because they're not part of the last 6 characters.
hex.toString(16) converts hex into string number representation in base 16. Then it appends 000000 at the beginning of the string to make sure it will be at least of length 6. and substr(-6) takes last 6 chars of the resulting string. This way you always get # + 6 hex chars. Which represents color.
The code first picks a random number and using the "& 0xFFFFFF" technique it ensures the range is something like 0 to 16777215.
Once we have that random number we convert to hexadecimal using the ".toString(16)" method, the 16 signifying we want hexadecimal conversion.
Now, we can think we have a 6 digit random number in hex to use for our color but know that the ".toString(16)" method does not do any padding for us.
For example, if the random number is 255 which is FF in hex, is not usable as it since it is not precisely 6 digits long.
One technique is to do a string length check and add the corresponding number of 0's to the beginning of the 'FF' to get '0000FF'.
Here we see another technique where you see a fixed number of 0's added to the string and then a fixed length is chopped of the end, ensuring you get 6 digits and correctly padded.
I've always used the string length check or specific padding functions (I don't know if javascript has one) - I only answered the question so as to fully appreciate the technique shown in this question.
/* a complete html page to apply this */
<html>
<body>
<button type="button" onclick="setbodybgcolor()">Random Background</button>
<script>
function setbodybgcolor(){
document.body.style.backgroundColor=getRandomColor ();
}
function getRandomColor () {
var hex = Math.floor(Math.random() * 0xFFFFFF);
return "#" + (hex.toString(16)).substr(-6);
}
/* we can do this also
function setbodybgcolor(){
var hex=Math.floor(Math.random()*16777215).toString(16);
document.body.style.backgroundColor="#"+hex;
}*/
</script>
</body>
</html>

Random number seeded with custom time

I am trying to create a random number as below using javascript
function valid(form) {
var input = 0;
var input = document.getElementById('custom1').value;
var final_input = input.charAt(0);
var number = 1000000000 - Math.floor(Math.random() * 1000000000);
final_input = final_input + number;
document.getElementById('custom4').value = final_input;
}
The idea is that it will get the value from "custom1" (which is one of the input fields) and then will get the first character. After that it will add next 9 random digits and puts the final value to custom4 (another input field) of the form. The javascript is working fine so far. However, I will rather have the random digit numbers be seeded with current time. I think that will it will be really random. Is that possible?
The JavaScript standard random API doesn't support explicit seeding (which is a shame). Here's the spec :
Returns a Number value with positive sign, greater than or equal to 0
but less than 1, chosen randomly or pseudo randomly with approximately
uniform distribution over that range, using an
implementation-dependent algorithm or strategy. This function takes no
arguments.
If you really need to seed your generator with a given number, you'll have to use a library like this one (not tested by me).
But the JavaScript random API is implicitly seeded, which ensures you'll have different results. There's no ECMAScript specification regarding how it's seeded but it's probable that all browsers use the time for the seeding. The MDN says
The random number generator is seeded from the current time, as in
Java.
I must correct myself. This is only correct for Mozilla (as far as i know): The JavaScript random API uses already the current time as a seed. No need to do it double (and it's not supported).
The specification doesn't mention a algorithm or strategies how the random number is generated and if and how it is seeded.
Thanks..
finally I have something like below
function valid(form) {
var input = 0;
var input = document.getElementById('custom1').value;
var final_input = input.charAt(0);
var number = new Date().valueOf(); /*1000000000 - Math.floor(Math.random() * 1000000000);*/
final_input = final_input + number;
document.getElementById('custom4').value = final_input;
}

Categories

Resources