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

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

Related

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.

Weighted Random Number Generator in Javascript

I am using the following code to generate a random number:
function getRandomInt (min, max) {
return Math.floor((Math.random() * (max - min + 1)) + min;
}
What I want to do is add a weighting that favours the numbers at the lower end of the range.
I thought about maybe trying to multiply the numbers by 1/cosine.
Would this work and does anyone know how I might go about it?
Many thanks!
First Solution
You need a function which contains the points (0, 0) and (1, 1). For instance: x^n when n > 0
Math.pow(1, n) === 1
And
Math.pow(0, n) === 0
Therefore, you would just change n depending on how you want the weighting to work.
When n = 1 : y === x
When n > 1 : y <= x
When 0 < n < 1 : y >= x
So, if you want lower values to be favored over higher values, simply use n > 1.
var weighted = Math.pow(Math.random(), 2);
Then you can scale the result as usual.
var scaled = Math.floor(weighted * (max - min + 1)) + min;
Other Functions
Likewise, you could use any continuous function which contains the points (0, 0), (1, 1), and has range and domain of [0, 1].
Sine
y = sin(xπ/2)
Cosine
y = 1 - cos(xπ/2)
EDIT: there was a type in the final formula, log(2+log(x)) is incorrect it should have been log(1+log(x))+1, its fixed now.
If you are using logarithmic weighting, using something like
var x = Math.random();
var weighted = x * Math.log(1+x);
would make 0.5 weigh in at around 0.2, but 1 would only weigh in at around 0.69.
Using this
var x = Math.random();
var weighted = x * Math.log(2 + Math.log(x));
would allow 1 to weigh in at 1. So combine them, and this
var x = Math.random();
var weighted = (x <= 0.5) ? x * Math.log(1 + x) : x * Math.log(1 + Math.log(x))+1;
should do the trick

Math random numbers between 50 and 80

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.

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