In Javascript, how do I create a random even number multiplied by 20 between 0 - 580?
E.g.: 220, 360, 180, 0 (min), 400, 200, 580 (max)
You want increments of 20, so what you really need is an integer in the range 0 to 29, and then multiply with 20. Example:
var max = (580/20) + 1;
var result = 20 * (Math.floor(Math.random())*max)
We are adding one to max, because Math.random() is a uniformly distributed number between (inclusive 0) and (exclusive 1), so since we use Math.floor, the maximum must be 1 larger.
This way creates a random number, then rounds it down to the nearest multiple:
When you need 0 <= randomMultiple <= max
var random = Math.random() * (580 + 20);
randomMultiple = random - (random % 20);
When you need 0 <= randomMultiple < max
var random = Math.random() * 580;
randomMultiple = random - (random % 20);
Use a principle like this: Generate random number between two numbers in JavaScript
Keeping in mind that if you want your max result to be 580, then the maximum integer you want to multiply by 20 would be 29 (or 580/20). Then just add some logic to make sure the integer is even.
Ta da!
Try use this:
var result = parseInt(Math.random()*30)*20;
29*20 = 580
Math.random() return [0..1)
result between 0..580, step by 20
Here is a generic javascript one-liner that can be used for any range, and any multiple.
Essentially, what we are trying to do here is figure out a range, 0 to N, which when multiplied by our given multiple stays within the range [0,max-min].
In this case N, is simply, (max - min)/multiple, or range/multiple.
Once we have N, we can use Math.random() to get a random number between 0-N, and multiply it with multiple. Next, we just add min.
We assume that min and max are already multiples of multiple.
Note the additional +1 to the input of Math.random() is because Math.random() returns a number between 0 (inclusive) and 1 (exclusive). So, Math.random() can never return 1. If we didn't account for that we would never be able to include the max number in our results.
/*
* Returns a random number within range [min,max]
*
* min and max must be multiples of multiple
* (note that 0 is a multiple of all integers)
*/
function randomMultiple (min, max, multiple) {
return Math.floor(Math.random() * (((max - min)/multiple)+1)) * multiple + min;
}
console.log(randomMultiple(0, 580, 20));
Related
I have an array (1200 values) of numbers
[123, 145, 158, 133...]
I'd like to have a div for each value with a background color from red to green, red being the smallest number and green the largest.
The base setup looks like this: (templating with vuejs but unrelated to the problem)
const values = [123, 145, 158, 133...]; // 1200 values inside
const total = values.length;
<div
v-for="(val, i) in values"
:key="i"
:style="{backgroundColor: `rgb(${(100 - (val*100/total)) * 256}, ${(val*100/total) * 256}, 0)`}">
{{val}}
</div>
I'm not a maths specialist but since all my numbers are around 100, the rgb generated is the same. (around 12% yellowish color)
How can I give more weight to the difference between 137 and 147?
EDIT: final formula:
:style="{backgroundColor: `rgb(${(256/(maxValue-minValue) * (boule-maxValue) - 255)}, ${(256/20 * (boule-maxValue) + 255)}, 0)`}"
Checkout this post: https://stats.stackexchange.com/questions/70801/how-to-normalize-data-to-0-1-range.
Basically you want to linearly rescale your values to another interval. You need your current min and max values from the array. Then define the new min' and max' which are the limits of the new interval. This would be [0, 255] in your case.
To do the transformation use the formula:
newvalue= (max'-min')/(max-min)*(value-max)+max'
As an example:
If your min value is 127 and max is 147, and you want to map 137. Then:
256/20 * (137-147) + 255 which results in 127.
If you want to map 130. Then:
256/20 * (130-147) + 255 = 37.4.
It really depends on what meaning those values actually have
However, you can try this: if your values are always bigger than 100 and always less than 150 (you can choose these number of course) you can "stretch" your values using the values as minimum and maximum. Let's take 137 and 147 as examples:
(val-min) : (max-min) = x : 255
(137-100):(150-100) = x:255 -> 37:50 = x:255 -> 188
(147-100):(150-100) = x:255 -> 47:50 = x:255 -> 239
That is for the math. In the end, this is the calculation:
newValue = (val-min)*255/(max-min)
where min and max are your chosen values.
You could take a kind of magnifier for a range of data. In this example, the values between 20 and 30 are mapped to a two times greater range than the outside values inside of an interval of 0 ... 100.
function magnifier(value, start, end, factor) {
var middle = (start + end) / 2,
size = (end - start) * factor / 2,
left = middle - size,
right = middle + size;
if (value <= start) return value * left / start;
if (value <= end) return (value - start) * factor + left;
return (value - end) * (100 - right) / (100 - end) + right;
}
var i;
for (i = 0; i <= 100; i += 5) {
console.log(i, magnifier(i, 20, 30, 2));
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
In Javascript, when I am scrolling, I would like to get 0 - 1 when the scroll.x number is from 300 - 400.
So 300 would be 0, then 400 would be 1, and in between would be 0.1, 0.2.
I am attempting to try in the meantime and will post my attempt as well.
Yep, you'll just want a simple remapping function.
const remap = (
value,
sourceMin,
sourceMax,
destMin = 0,
destMax = 1,
) =>
destMin +
((value - sourceMin) / (sourceMax - sourceMin)) *
(destMax - destMin);
console.log(remap(300, 300, 400));
console.log(remap(400, 300, 400));
console.log(remap(350, 300, 400));
outputs
0
1
0.5
Note that this function does not clamp the output values to the range you specify; if you specify out-of-range inputs, you'll get out-of-range outputs.
You could subtract the start value and divide by 100 (the delta of the two values) for getting a value between zero and one.
function f(x) {
return (x - 300) / 100;
}
console.log(f(300)); // 0
console.log(f(350)); // 0.5
console.log(f(400)); // 1
I have a function that calculates percentage increase of 2 numbers:
const 1st_num = 50
const 2nd_num = 100
percentage = ((1st_num - 2nd_num) / 1st_num) * 100 // -100
It seems correct but what if the 1st number is 1?
((1 - 50) / 1) * 100 // -4900
I don't see it making sense anymore. What am I missing?
If you are computing a delta variation in percentage between 2 numbers, it should be the other way around:
variation = ((num2 - num1) / num1) * 100
Last but not least, your delta can be over 100%
For example, imagine at
t1=10 and t2=11 -> your delta will be computed like this : (11 - 10)/10, so you have an increase of 10%
but if you have t1=10 and t2=100 -> your delta will become (100 - 10)/10, so you have an increase of 900%
First up all your question is more suitable to somewhere in math forums:
Your formula is right just change it as follows to get increase change in positive numbers:
percentage = ((2nd_num - 1st_num) / 1st_num) * 100 // 100%
However your treatment with 1 is exactly right.
4900 % In other words 49 times increase in value.
You can't use variable name starts with numbers
const fst_num = 50
const snd_num = 100
percentage = ((snd_num -fst_num) / fst_num) * 100
const fst_num = 70192.32
const snd_num = 17548.08
const percentage = ( 100 - ( ( fst_num - snd_num ) / fst_num ) * 100 );
I'm trying to generate random integers that are are multiples of 30 in JavaScript.
That is:
0 60 0 180 120 ...... and so on
in range between 0 to 360 for example
So I am looking for a function something like this:
function (_range,_multi)
{
Math.round(...);
return rndNum;
}
Generate a random number between 0 and 12 (range) and multiply by 30 (multi):
Math.floor(Math.random() * 12) * 30
This gives you [0, 360) (so you never get 360)
Here's a live demo that shows a full working function - the general idea is that you multiply by (max/multiple), floor the value, then multiply it by the multiple:
function generate(min, max, multiple) {
var res = Math.floor(Math.random() * ((max - min) / multiple)) * multiple + min;
return res;
}
alert(generate(0, 360, 30));
Seems like that should work.
function randomMultiple(max, mult) {
return Math.floor(Math.random() * (max / mult)) * mult;
}
Thus a call of randomMultiple(360, 30) would produce an element of G with
G = { y = 30 * x | 0 < x < 12 }
so I have this code that is giving me a random number for both top and left attribute of some images.
var random1 = Math.ceil(Math.random() * 500);
var random2 = Math.ceil(Math.random() * 500);
$(document).ready(function () {
$('#randomp').css('top', random1);
$('#randomp').css('left', random2);
});
The problem is that I would be prefer to randomize a number between 1 and 100%. Is that possible?
Math.floor(Math.random() * 100) + 1 + '%'
Since Math.random returns number that is random, not less than 0 and less than 1, you have just to multiply result by 99 instead of 500 to get a number beetween 1 and 100%.
Finally, the code should be as follows:
var random1 = Math.round(Math.random() * 99) + 1;
var random2 = Math.round(Math.random() * 99) + 1;
This gives you a number between 0 and 100 (both included):
Math.floor(Math.random() * 101);
Math random will give you a number between 0 (included) and 1 (excluded)
If you multiply that number with 101, it will give you a number between 0 (included) and 101 (excluded)
Math.floor will return the the largest integer less than or equal to the above number.
Not sure if you want it rounded or not, so here's both:
console.log( rando(1, 100) + "%" );
console.log( rando(1, 100, "float") + "%" );
<script src="https://randojs.com/1.0.0.js"></script>
This uses randojs.com to make the randomness simple and readable. If you need to know more, check out the website.