Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have ranges in hexadecimals like below.
range = "80000000-d554ffff"
range = "d5550000-2aa9ffff"
range = "2aaa0000-7fffffff"
I don't understand how to change above ranges in percentage.
Answer must be like - 50%, 25% ,25% not exactly this. answer as per data.
If min=80000000 and max=7fffffff then?
https://jsfiddle.net/amitkushwaha1710/wk4786tr/
You could get the decimal values, take the delta and get the percent value.
var ranges = ["80000000-d554ffff", "d5550000-2aa9ffff", "2aaa0000-7fffffff"],
result = ranges.map(s => {
var [l, r] = s.split('-').map(v => parseInt(v, 16));
return (r - l) * 100 / l;
});
console.log(result);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have array like this for example:
const arr = [3,5,7,10];
And I have a number, for example:
const status = 4;
I have to find index of this number, so number 4 is bigger than 3, and less then 5, so it belongs to second index, I have to get something like position.
Position for the number for should be 2. (it's bigger than 3, but less then 5).
Expected result
const position = 2;
are you looking for this simple solution?
const arr = [3,5,7,10];
const status = 4;
const position = arr.findIndex( e => e > status) + 1;
console.log(position)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array of agents which each have the attribute rate which is their accepted calls minus their declined calls. I order these agents in the array based on the rate. I'd like to give agents with a better rate a higher chance to be selected for a call than other agents with a lower rate to make things fair. I understand how to select a random element from an array but I can't wrap my head around this problem.
In this code, there is a 2/15 chance to get 2, 3/15 chance to get 3, and so on.
15 is based on the total rate of all the agents.
var aoa = [{rate: 2},{rate: 3},{rate: 6},{rate: 4}];
var r = Math.random() * aoa.map(e => e.rate).reduce((ac,c) => ac+c,0);
var sa;
aoa.forEach((a,i) => {
var pt = aoa.slice(0,i).map(e => e.rate).reduce((ac,c) => ac+c,0);
if (r >= pt && r < pt+a.rate) sa = a;
console.log(`${r} has to be between ${pt} and ${pt+a.rate} if chosen rate = ${a.rate}`);
});
console.log(sa);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to generate random numbers between 0 and 9 in JavaScript without using the builtin function math.random(). I need to find out a solution for this problem.
You could just use the mouse position and the time as random noise:
// Expose the current mouse position
let clientX = 0;
window.onmousemove = e => clientX = e.clientX;
// Generate random values and show them:
setInterval(() => {
document.body.textContent = (+new Date + clientX) % 10;
}, 100);
(Note that this is not random enough to be used in cryptographic / security relevant code)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Var randomLetters = "ljhgfdza";
Var randomString = "";
Now I'll have to add elements in the first variable to the second Randomly
Using the while loop, and Math.floor(Math.random() * random letters.length)
I am having a problem at my
"While (condition)" what should it be?
To flat out answer your question, you can use:
while(randomLetters.length > 0){
Then when you use a letter from randomLetters, you delete the letter and the length is now 1 less.
This will be enough for you:
const randomLetters = "ljhgfdza";
const returnRandom = (randomString) => {
const arrString = [...randomString].sort((a, b) =>{
return 0.5 - Math.random()
}).join("");
console.log(typeof arrString,arrString);
}
returnRandom(randomLetters);
but... in this case sort method is not that random as you think. This link will tell you why. I would do this with reduce() or map(), both are described in link above.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have 40 fruits and I want to distribute it randomly and store in array of length 10.
See below code inputs.
var fruits = 40;
var arr = [];
And I want output like this
arr = [2,5,1,0,0,3,6,10,0,13];
10 positions of array should be filled randomly but addition of values should be 40.
A recursive divide-and-conquer approach:
function distribute(length, value) {
if (length <= 1)
return [value];
var half = Math.floor(length / 2),
dist = Math.floor(Math.random() * value);
return distribute(half, dist).concat(distribute(length-half, value-dist));
}