round number to 3 decimal points (only if necessary) - javascript

Instead of Math.round() and toFixed(), what is the alternative way to maintain 3 decimal if only divisible number contain decimal.
I want to have
100 / 30 => 3.333
390 / 100 => 3.90
100 / 100 => 1

You could fix the decimals and replace last zeroes with dot, if necessary.
function round(n) {
return function (v) {
return v.toFixed(n).replace(/\.?0+$/, '');
};
}
var array = [100 / 30, 390 / 100, 100 / 100, 100];
console.log(array.map(round(3)));
With a first keeping zero.
function round(n) {
return function (v) {
return v.toFixed(n).replace(/0+$/, '0').replace(/\.0+$/, '');
};
}
var array = [100 / 30, 390 / 100, 100 / 100, 100];
console.log(array.map(round(3)));

Related

random number between 1000 & 2000 for time of day

I'm looking to generate a random number between 1000 & 2000 for the time of day, it has to be in 100 increments. It's so I can test prebid timeouts and see different viewability figures and cpm's. Then once I get enough data, I can work out the best settings for any given time of day and hopefully get better results.
help would be much appreciated.
This is what i have so far but i'm changing them by hand depending on the time of day I see peaks and troughs in traffic at the moment:
var timeoutMap = {
0 : 2000,
1 : 2000,
2 : 2000,
3 : 1600,
4 : 1600,
5 : 1600,
6 : 1400,
7 : 1400,
8 : 1400,
9 : 1400,
10 : 1400,
11 : 1400,
12 : 1600,
13 : 1600,
14 : 1600,
15 : 1600,
16 : 1600,
17 : 1600,
18 : 1600,
19 : 1600,
20 : 1600,
21 : 1600,
22 : 1600,
23 : 2000
};
var t = new Date().getUTCHours();
PREBID_TIMEOUT = timeoutMap[t];
console.log("prebid timeout:", PREBID_TIMEOUT );
The general formula would be minValue + randomFloatBetweenZeroAndOne * (maxValue - minValue). Then to get the steps of 100 it would be randomNumGeneratedInPreviousStep / 100, rounded to nearest int, then multiplied by 100.
function generateRandom() {
const randomNum = 1000 + Math.random() * 1000;
return Math.round(randomNum / 100) * 100;
}
EDIT:
To make it more generic and allow you to customize aspects of this:
function generateRandom(min, max, step) {
const randomNum = min + Math.random() * (max - min);
return Math.round(randomNum / step) * step;
}
If you wanted to apply this to your object for each value in your object, I would not make it super generic unless you knew you wanted to use it elsewhere. If that were the case, I'd wrap the general function in a more specialized function that closed on your specified values for this specifc use-case and do something like this:
const MIN_RANDOM = 1000;
const MAX_RANDOM = 2000;
const RANDOM_STEP = 100;
function generateRandom(min, max, step) {
const randomNum = min + Math.random() * (max - min);
return Math.round(randomNum / step) * step;
}
function myRandom() {
return generateRandom(MIN_RANDOM, MAX_RANDOM, RANDOM_STEP);
}
var timeoutMap = {
0 : myRandom(),
1 : myRandom(),
2 : myRandom(),
3 : myRandom(),
...
};

How would i get get min Y value for graph?

I don't know how to explain this but i will try my best, so sorry in advance
For example this numbers
i would like to get
5 => 1, 12 => 10, 128 => 120, 1493 => 1400, 13301 => 13000
I have came up with Math.pow(10, Math.floor(Math.log10(x)));
which returns me 1, 10, 100, 1000, 10000
You could get the exponent and a factor and check if the value is smaller than 100, then take just the decimal with the exponent or the adjusted value.
function format(v) {
var e = Math.floor(Math.log10(v)),
f = 10 ** (e - 1);
return v < 100
? 10 ** e
: Math.floor(v / f) * f;
}
console.log(...[5, 12, 128, 1493, 13301, 239584].map(format));
// 1, 10, 120, 1400, 13000, 230000

How to get range from 0 - 1 based on two number range

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

How to generate random integers which are multiples of 30 in JavaScript

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 }

JavaScript - random multiple of 20 between 0 - 580

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

Categories