distribute numbers randomly in javascript [closed] - javascript

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

Related

JavaScript - Get array element position/index [closed]

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)

change hexadecimal range in percentage [closed]

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

Select random element based on numeric attribute value [closed]

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

While loop and Strings [closed]

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.

Returns the cube of the sum of an input and 2 [closed]

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'm taking online class to learn JS and I'm stuck. The quiz is asking me to:
Returns the cube of the sum of an input and 2.
So I coded this:
var returnCubed = function(num){ return (num + 2) * 3;};
But it doesn't pass. What else can I do to take in a parameter, add two to it, then cube it? Or am I not understanding the directions?
Try
var returnCubed = function(num){
return (num+2) * (num+2) * (num+2);
};
*3 will always multiply with 3.
Hope this can solve your problem.

Categories