random number between 1000 & 2000 for time of day - javascript

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(),
...
};

Related

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

round number to 3 decimal points (only if necessary)

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

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 }

Slight error in calculations

I'm trying to build a game somewhat like Wheel of Fortune, actually it's done but there seems to a weird issue I can't figure out.
Here's the wheel itself, as you can see the values are spaced evenly, each having a 15 ° slice.
To spin the wheel I generate a random number which will be the amount of rotation in degrees, I animate the wheel and everything is fine but when the rotation stops I have to get the value from the wheel which stops at the top-center position so I though this would solve it:
wheelValues = [
1000, 3250, 1800, 1000, 1200, 3750, 5000, 1000, 3000, 1600, 1000, 3500,
1000, 2000, 1000, 2750, 1000, 4000, -1, 1000, 2500, 1400, 1000, 2250
];
if (toAngle > 360)
{
toAngle = toAngle - (Math.floor(toAngle / 360) * 360);
}
arrIndex = Math.floor(toAngle / 15) + 1;
result = wheelValues[arrIndex];
where toAngle is the random spin I generate which can be between 370 and 1440.
This method works in about 8/9 times out of 10 and I can actually get the correct value the wheel stops at but I can't really understand why sometimes the value is off (and sometimes really off, not even near the correct value).
You're adding 1 to your array index for some reason.
array indexes start from 0.
Two problems I can see;
Firstly, this line:
if (toAngle > 360)
If toAngle == 360, I believe this will produce an error, as you will skip the 'modulus' section and toAngle will finally be 24 (larger than your dataset)
Second:
arrIndex = Math.floor(toAngle / 15) + 1;
No need to +1 this, as you will never get your first value, and on occasion you will exceed the array bounds.
Also, as to why you get odd values, have you tried to write some simple code to debug your assumptions? Write a loop, that iterates from 370 to 1440 (your stated input boundaries) and see what your calculation comes up with for each value. Print it to a file, or screen and you can quickly scan to see where the issues might be.
BTW, it's probably best if you make your random number an even multiple of 15 degrees in the first place, then you don't need all that rounding and flooring, e.g.
function randDeg() {
var epoch = 360; // minimum value
var max = 1440; // maximum value to return
var step = 15; // even multiple to return
var t = Math.random() * (max - epoch) | 0;
t -= t % 15;
return t + epoch;
}
Will return a number n where epoch <= n <= max and n%15 = 0.

Categories