I'm not certain what the correct term is to calculate this formula; and so my apologies if the title seems a bit vague.
My question pertains to programming the formula in JavaScript; however for the sake of simplifying the objective; the following scenario applies:
Imagine my bakery has an unlimited quantity of pies. So I advertise that anyone can walk in and grab a pie for free; and so an indeterminate number of people start arriving at my bakery to collect a free pie.
With still an unlimited supply in quantity; I decide that, of all the people to collect a pie (indeterminate amount of people - could be 1, 2, 3 or even 1000000), I want 70% of people to get a blueberry pie, while the other 30% of people get an apple pie.
How would I go about calculating the percentage allocation without knowing the quantity of people that would be collecting a pie?
My thought process is that, I would give a blueberry pie to each person up to the point where 7 people have collected a blueberry pie. For the next 3 people, they'd get an apple pie.
But what if (for argument's sake), only 9 people in total arrive at my bakery? All 9 people would have gotten a blueberry pie, and no one would have gotten an apple pie?
To confuse things even more (not necessary, quite happy just calculating the rate from a linear / non-random approach): What if I don't want to seem too obvious, and with the same rates (70/30) I wish to allocate the pies randomly? (A = blueberry pie, B = apple pie):
So instead of:
A,A,A,A,A,A,A,B,B,B
it may just as well be:
A,A,B,A,B,B,A,A,A,A
Also, what would be the correct term / name to refer to this type of formula?
CodePudding user response:
If you want the numbers based on just probability (70% and 30%) not like, out of 100 people exact 70 people(70%) must get Blueberry and other 30 people Apple pie (meaning 72 and 28 NOPE!!). Then here is a simple solution:
The simplest way would be with Math.random()
(it gives a number from 0 to 0.9 randomly, ex: 0.2374, 0.6789, 0 . . . . . but never 1).
First get Math.random()
and multiply it by 10 and use Math.floor()
to make it a whole number (integer). Then if you get any number from 0 to 6 (0,1,2,3,4,5 and 6) give Blueberry and else (7,8 and 9) give Apple pie.
const array = []
// Make a for Loop for N number of peoples
for (i=0; i<100; i ){
let pies;
// Get a whole random number (0 to 9)
let randomN= Math.floor(Math.random() * 10)
// Write the probabilities
if (randomN == 0) pies = "Apie"
else if (randomN == 1) pies = "Apie"
else if (randomN == 2) pies = "Apie"
else pies = "Bpie"
//Make a list with array
array.push(pies);
}
// Let's check the results
function arrayEcounter(array) {
let Apie = 0;
let Bpie = 0;
for ( i= array.length-1; i >= 0; i--) {
if (array[i] == "Apie") Apie = 1;
else if (array[i] == "Bpie") Bpie = 1;
}
return `${Apie} people got Apple pie
and ${Bpie} people got Blueberry pie.
You have gave away total ${Apie Bpie} pies`
}
console.log(arrayEcounter(array))
CONSOLE: 27 people got Apple pie and 73 people got Blueberry pie. You have gave away total 100 pies
300276 people got Apple pie and 699724 people got Blueberry pie. You have gave away total 1000000 pies
CodePudding user response:
Since you know the percentage distribution the actual number of people is irrelevant. But, you do need to know how many pies to "prepare" per x number of people so that you can create a random distribution of pie A to Pie B
function CreatePieDistribution(applePercentage, perNPeople) {
let numApplePiesPerNPeople = perNPeople * applePercentage;
for (let i = 0; i < perNPeople; i ) {
pies.push( createPie(i, numApplePiesPerNPeople));
}
pies.sort((a, b) => a.sortby - b.sortby);
}
function createPie(pId, numApplePies) {
let pie = {
id: pId,
type: null,
sortby: null
};
if (pId < numApplePies)
pie.type = "apple"
else
pie.type = "blueberry"
pie.sortby = Math.random();
return pie;
}
let pies = [];
let pctApple = .3;
let batchSize = 10;
CreatePieDistribution(pctApple, batchSize)
console.log(pies);