Math random numbers between 50 and 80 - javascript

I know there's several questions on this, but I'm struggling to find out how to use Math.random to get random numbers between two high integers?
So, for example, between 50 and 80. I thought this would work...
'left': Math.floor((Math.random() * 80) + 50) + '%'
Any ideas?

Assuming the range is inclusive on both ends:
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}

You need to know the range of the random.
Between 50 and 80, the range is 30 (80 - 50 = 30), then you add 1.
Therefor, the random would look like this :
Math.floor(Math.random() * 31) + 50

Here consider your min=50 and max = 80 refer below code:
var number = Math.floor(Math.random() * (max - min) + min);
This will solve your problem. You can try any range by changing min and max.

Related

Accessing a random variable from an array for use in another varaible

I have one of four possibilities for this particular variable -->
var lowVelocity = Math.random() * (45 - 30) + 30;
var medVelocity = Math.random() * (60 - 45) + 45;
var highVelocity = Math.random() * (80 - 60) + 45;
var hwyVelcoity = Math.random() * (100 - 80) +80;
In one scenario I might have either lowVelocity and medVelocity so I want to choose randomly between those two only. So I did this by:
const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);
And this works - it now chooses one of the two velocities that I am interested in. Low velocity is between 30 and 45 and med is between 45 and 60.
In my innerHTML I want to print the number returned from that variable chosen in the array. When the user clicks this starts a function that has this in it.
document.getElementById("scenario").innerHTML = randomUrban;
But when it prints in the HTML, it doesn't print the number that is associated with the variable, it prints the array number it chose (like 0 or 1).
How do I get the variable (eg if it chose lowVelocity then the number it prints will be the random number lowVelocity found, eg 39) to print instead of the array number?
You could just use a single array to do all of this for you!
If you add the velocities to an array, then the random number can be used as an index into this array. In the code below, we use the length of this array to set the limits for the random number, then you can simply access the value from the velocities array at the position indicated by the random number:
velocities[randomUrban][1]
This gets the sub-array at the position indicated by randomUrban, and [1] gets the second element in the row, i.e. the calculated velocity.
/* Add the velocities to an array or easier management */
var velocities= [
["lowVelocity", Math.random() * (45 - 30) + 30],
["medVelocity", Math.random() * (60 - 45) + 45],
["highVelocity",Math.random() * (80 - 60) + 45],
["hwyVelcoity" ,Math.random() * (100 - 80) +80]
]
/* Pick a random position from the velocities */
const randomUrban = Math.floor(Math.random() * velocities.length);
/* Print the velocity at that position. (Also includes the velocity name for illustration) */
document.getElementById("scenario").innerHTML =
velocities[randomUrban][0] + " = " + velocities[randomUrban][1];
<div id="scenario"></div>
Note: In your example you only use 2 of the 4 velocities, so you can just add the velocities you want to choose from into the randomVelocities array.
In your case just use
document.getElementById("scenario").innerHTML = window[velocities[randomUrban]];
var lowVelocity = Math.random() * (45 - 30) + 30;
var medVelocity = Math.random() * (60 - 45) + 45;
var highVelocity = Math.random() * (80 - 60) + 45;
var hwyVelcoity = Math.random() * (100 - 80) + 80;
const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);
console.log(randomUrban);// random from velocities
console.log(velocities[randomUrban]); //get variable name
console.log(window[velocities[randomUrban]]); // search in scope where the variables are declared in this case window
How about document.getElementById("scenario").innerHTML = velocities[randomUrban]; ?
Well, you are trying to print the wrong variable; randomUrban is the variable containing the velocities array index selected.
A quick (but pretty dirty) fix would be printing eval(velocities[randomUrban]) but eval() has been deprecated because of security issues.
A bit longer but better solution would be grouping your velocities into a single variable of type object and access its keys to retrieve the numeric value:
const velObj = {
lowVelocity: Math.random() * (45 - 30) + 30,
medVelocity: Math.random() * (60 - 45) + 45,
highVelocity: Math.random() * (80 - 60) + 45,
hwyVelocity: Math.random() * (100 - 80) + 80
};
const velocities = ["lowVelocity", "medVelocity"];
const randomUrban = Math.floor(Math.random() * velocities.length);
const velKey = velocities[randomUrban];
document.getElementById("scenario").innerHTML = velObj[velKey];
<div id="scenario"></div>

Generating random number in javascript [duplicate]

This question already has answers here:
Javascript Random Number?
(3 answers)
Closed 3 years ago.
i am using this function to generate random number between 1000 and 100.
but here according to me, in (max - min) + min, max- min =900 and min= 100, so it should not generate numbers between 900 and 100? but it is returning numbers greater than 900 also how? I am confused. and do tell how to check the range for the numbers random function is generating? any help with this?
x = Math.floor(Math.random() * (1000 - 100) + 100);
console.log(x);
The formula for random numbers Math.random() * (max - min) + min is the correct one to get a uniformly distributed number between min and max.
max - min will give you the range in which you want to generate the random numbers. So in this case 1000 - 100 results in a range of 900.
Multiplying by Math.random() will give you a random number in the range. So, with a Math.random() producing 0.5 after multiplying you get 450.
Finally, adding min back to the random pick ensures the number you get is within bounds of min and max.
For example Math.random() produces 0.01 if we substitute in the formula we get 0.01 * (1000 - 100) = 9 which is below min. Conversely, if Math.random() produces 1 then 1 * (1000 - 100) = 900 which is the highest random number possible to get from the range and yet it's still below max. In both cases adding min to the result ensures the random number you get is within max and min
The function Math.random() returns a number between 0 and 1.
When use "Math.random() * (1000 - 100)", this part of the code generates a number between 0 and 1 then multiplies it by 900, which will give you a number between 0 and 900.
Now in the last block you do add 100 to the previously generated number which results in a number between 0 and 900 + 100, which gives a result between 100 and 1000.
function random(min, max) {
console.log("Multiplying by: " + (max - min));
console.log("And adding : " + min);
return Math.floor(Math.random() * (max - min) + min);
}
console.log(random(100, 1000));
Multiply by (1000 -200) instead as you already have +100
Because in case random number generated is anything greater than 800 you end exceeding range as you're adding 100 in it everytime
x = Math.floor(Math.random() * (1000 - 200) + 100);
console.log(x);
Thumb rule :-
Math.floor(Math.random() * - ( max - ( 2 * min ) ) + min )
As Math.random() generate floats, this need to be converted to an integer.
We can use parseInt(), but there is a shorthand, the ~~ bitwise operator. Performances are known to be excellent.
console.log(
100 + ~~(Math.random() * 800)
)
One possible alternative is the web crypto api, it might be a bit slower, but with the best randomness doable. This return an integer between 0 and 256.
console.log(
100 + ~~(crypto.getRandomValues(new Uint8Array(1))[0] * 3.13)
)

How to use Math.float for a damage formula equation

I have found a guide for some damage formulas from FF9 and want to use them
in RMMV which uses Javascript.
Im bad at math and dont know how the Math.random and modulo
works or at least im doing something wrong.
I allways get 0 damage and im sure its because of wrong equation.
Since the Math.random gives a float from 0.00 to 1.00 I thought that would be the problem.
So I've tried to use a random number between 1 and 100 but that didnt helped.
Base = Spell Power - Mag Def
Bonus = Mag + Rnd MOD ([(Lvl + Mag) / 8] + 1)
Damage = Base * Bonus
SPELLPOWER - b.mdf * (a.mat + (Math.random() % ((a.level + a.mat) / 8) + 1))
16 - 2 * (16 + (Math.random() % ((1 + 16) / 8) + 1))
SPELLPOWER - b.mdf * (a.mat + ((Math.random() * (100 - 1) + 1) % ((a.level + a.mat) / 8) + 1))
16 - 2 * (16 + ((Math.random() * (100 - 1) + 1) % ((1 + 16) / 8) + 1))
Somehow this should actualy give a number higher then 0 with the stats i provide.
It was just some wrong set brackets which caused the trouble.
(SPELLPOWER - b.mdf) * (a.mat + Math.random() % (((a.level + a.mat) / 8) + 1))
This works perfectly fine now.

var flip = Math.floor(Math.random() * (1 - 0 + 1)) + 0;?

I found some code snippets and I'm not sure what it means:
var flip = Math.floor(Math.random() * (1 - 0 + 1)) + 0;
and
Math.floor(Math.random() * (max - min + 1)) + min
I'm still getting familiar with Javascript... could someone give me a reader's digest condensed version?
Math.floor() Round a number downward to its nearest integer
For example: if you have 2.7 it will round down to 2.
Math.random() Return a random number between 0 and 1
For example: .693

JavaScript: Round to the nearest value on a scale

Lets say I have a scale with 10 values between a know min and max value. How can I get the nearest value on the scale for value between min and max. Example:
min = 0, max = 10, value = 2.75 -> expected: value = 3
min = 5, max = 6, value = 5.12 -> expected: value = 5.1
min = 0, max = 1, value = 0.06 -> expected: value = 0.1
You could use something like this
function nearest(value, min, max, steps) {
var zerone = Math.round((value - min) * steps / (max - min)) / steps; // bring to 0-1 range
zerone = Math.min(Math.max(zerone, 0), 1) // keep in range in case value is off limits
return zerone * (max - min) + min;
}
console.log(nearest(2.75, 0, 10, 10)); // 3
console.log(nearest(5.12, 5, 6, 10)); // 5.1
console.log(nearest(0.06, 0, 1, 10)); // 0.1
Demo at http://jsfiddle.net/gaby/4RN37/1/
Your scenario doesn't make much sense to me. Why does .06 round to 1 and not .1 but 5.12 rounds to 5.1 with the same scale (1 integer)? It's confusing.
Either way, if you want to round to a precise # of decimal places, check this out:
http://www.javascriptkit.com/javatutors/round.shtml
var original=28.453
1) //round "original" to two decimals
var result=Math.round(original*100)/100 //returns 28.45
2) // round "original" to 1 decimal
var result=Math.round(original*10)/10 //returns 28.5
3) //round 8.111111 to 3 decimals
var result=Math.round(8.111111*1000)/1000 //returns 8.111
With this tutorial, you should be able to do exactly what you want.
Perhaps more comprehensible:
var numberOfSteps = 10;
var step = (max - min) / numberOfSteps;
var difference = start - min;
var stepsToDifference = Math.round(difference / step);
var answer = min + step * stepsToDifference;
This also allows you to change the number of steps in your sequence.
I suggest something like that :
var step = (max - min) / 10;
return Math.round(value / step) * step;
I had the problem where I was getting 5.7999997 instead of the weanted 5.8 for example. Here was my first fix (for java...).
public static float nearest(float val, float min, float max, int steps) {
float step = (max - min) / steps;
float diff = val - min;
float steps_to_diff = round(diff / step);
float answer = min + step * steps_to_diff;
answer = ((int) (answer * steps)) / (float) steps;
return answer;
}
However using this on nearest(6.5098, 0, 10, 1000) I would get 6.509 instead of the wanted 6.51.
This solved it for me (watch out for overflows when values are really large):
public static float nearest(float val, float min, float max, int steps) {
val *= steps;
min *= steps;
max *= steps;
float step = (max - min) / steps;
float diff = val - min;
float steps_to_diff = round(diff / step);
float answer = min + step * steps_to_diff;
return answer / (float) steps;
}
var step = 10;
return Math.ceil(x / step) * step;

Categories

Resources