Create an array in JavaScript that represents a normal distribution based on the provided minimum, maximum, mean, and sample size

Consider the following scenario:

min = 1
max = 5
Ave = 3
size = 5

At this stage, we have [1,?,?,?,5]

How can I determine the missing numbers? (It's simple -> [1,2,3,4,5] - but how do I create a JavaScript function to return this array)

Alternatively, given:

min = 23
max = 2500
Ave = 1007
size = 800

Now we have [23,?..,2500]

How can we calculate the missing numbers to achieve a uniform distribution?

Regarding the returned Array

  1. The numbers must be rounded to 2 decimal places.
  2. Each number in the array is greater than or equal to the preceding number.
  3. The plotted distribution should resemble a shallow S shaped curve.

So far, here's what has been done:

const min = 1;      //lowest value
const max = 5;      
const ave = 4;   
const supply = 3;   

const data = Array.from({length: supply}, (_, i) => i + 1);
const distArray = [];
data.map(n => {
    distArray.push(Math.round((min+n*(max-min) / (supply+1)) * 1e2 ) / 1e2);
});
distArray.unshift(min);
distArray.push(max);
console.log(distArray); // returns [1,2,3,4,5]

The function above generates an array of length supply+2 between the min and max values.

The next step involves incorporating the ave variable. What if the average of the 5 numbers was now 2.5 instead of 3? This adjustment would require a different approach... How can this be achieved?

An attempt has been made below, albeit inaccurate for larger arrays. The current formula seems to concentrate the distribution around the middle. A more effective method is needed...

const min = 5;  
const max = 12;     
const supply = 5;   
const ave = 8;

const data = Array.from({length: supply}, (_, i) => i + 1);
const distArray = [];

const ksum = (supply + 2) * ave; 
const usum = ksum - (min + max); 
const parts = data.reduce((a, n) => a + n, 0);
const b = Math.floor(usum / supply); 
const r = usum % supply; 

data.map(n => {
    distArray.push(Math.round((b + (n/parts * r)) * 1e2 ) / 1e2);
});
distArray.unshift(min);
distArray.push(max);

console.log(distArray); // returns [5, 7.27, 7.53, 7.8, 8.07, 8.33, 12]

Answer №1

Let's dive into some mathematical calculations:

avg = sum(all) / size
sum(all) = sum(known) + sum(unknown)
sum(unknown) = avg x size - sum(known)
unknown = sum(unknown) / (size - size(known))

Do you think this approach would be suitable for your situation?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to enhance a state's capabilities in Machina.js?

When using Machina.js (version 0.3.6), how can I instantiate a modified FSM constructor where both the child and parent FSMs define behaviors in the same states? Here is the code snippet: var _ = require('lodash'); var machina = require('m ...

Switching between height: 0 and height:auto dynamically with the power of JavaScript and VueJS

Currently, I am changing the height of a container from 0px to auto. Since I do not know the exact final height needed for the container, using max-height could be an option but I prefer this method. The transition from 0 to auto works smoothly, however, ...

Manipulating the information pulled from an external PHP script that is currently being displayed

Although I don't consider myself a professional, I am determined to learn some web languages this summer to be able to turn my design ideas into prototypes. Currently, I am struggling to figure out how to manipulate elements that are being echoed bac ...

`Is there a way to dynamically update a nested field object in Mongoose without updating the entire object?`

const userProfile = new Schema({ name: { type: String, default: null }, contacts: { mobileNumber: { countryCode: { type: String, default: null }, digits: { type: String, default: null } }, email: { type: String, default: null }, facebook: { ...

Having issues with jQuery not functioning on this specific web page across all browsers. The reason behind this puzzling dilemma remains elusive to me

I am experiencing issues with the functionality of my website. The buttons with + signs are meant to be drop-down toggles, and the mini-tabs below them should work as tabs. Additionally, the sample images at the bottom are not loading properly when clicked ...

The issue of "Next.js localStorage not being defined persists, despite attempting to

Despite encountering numerous similar questions, I have not been able to find a solution to my specific issue. This is my first experience utilizing Next.js and TypeScript. I am conducting a mock login with REQRES and storing the token in the localStorage ...

Neglecting to automatically align text

My goal is to automatically align text based on the language, so that Arabic text starts from the right and English text starts from the left. After some online research, I discovered that I need to use dir="auto" in the tag and text-align: auto; in the CS ...

Ways to categorize an array into subarrays based on name

I am working with an array format: array ( 0 => array ( id = 1, name = chimpanzee, address = Singapore ) 1 => array ( id = 2, name = meeting, address = USA ) 2 => array ( id = 3, name = dynasty, address = Singapore ) 3 => ...

Utilizing Jquery to Pass an Optional Function to Another Function

I am currently working on a function that utilizes AJAX to submit data and then displays a dialog indicating whether the process was successful or not. Everything seems to be functioning smoothly, but I now want to add the capability of passing an addition ...

Adding query parameters dynamically in Vue without refreshing the component

I'm attempting to update the Query Parameters in Vue without refreshing the component using Vue-Router. However, when I use the code below, it forces a component reload: this.$router.replace({query: this.filters}); Is there a way to prevent the comp ...

The login button has dual functionality, redirecting users to different pages based on their login status. First-time logins will be directed to an agreement page, while returning users will be

During my testing of login functionality, I follow these steps: Enter username Enter password Click on the login button Verify if the agreement page is displayed This is how my Page Object Model (POM) for the login page looks like: class loginPage { ...

Create a responsive canvas with custom shapes

After designing a canvas with a gradient-filled circle, I encountered a challenge in making the canvas and the circle responsive to varying screen sizes. Initially, I attempted to use `vh` and `vw` units for the width and height of the canvas. However, ad ...

A conflict with the Ajax file is causing an automatic logout

In my Rails application, there is a page with a table that uses partial AJAX to increase the capacity attribute in a time entity. You can view the visual representation of the table here. By clicking the plus/minus button, the capacity attribute increases ...

Ways to retrieve information when text is modified and a dropdown is selected within an input field

I have an input field with a text box and a dropdown. My goal is to trigger data based on text changes in one method, while using another method for the dropdown. Check out the code below. const allList = [ { id: "1", value: "Fruits" ...

Utilizing Javascript to share images from a public Facebook page to a user's timeline via the Facebook API

Can someone assist me with finding a way to share a photo from a public Facebook page to the user's timeline using JavaScript? Is it possible to achieve this with FB.api or FB.ui? I have successfully shared feeds to the timeline using FB.ui, but am no ...

Node.js Link Management: A Guide to Updating Links

For some time now, I've been attempting to update the tabs on my navigation bar (including the links) when a user logs in. The issue arises on the index page, where users can access it both when logged in and not logged in. In my navigation bar, all t ...

How can I determine if the checkbox is selected while iterating through my table?

Here is the code I use to construct my table: for (var i = 0; i < result.length; i++) { var item = result[i]; // Firma, BygningselementNavn, BrugerNavn, EmailAdresse, Telefon tbody = tbody + '<tr class="modtagerRow"><td>&a ...

How can I retrieve the attributes of multiple identical components within a webpage?

I've recently delved into learning Vue and decided to create a small application for adding fractions together. I have developed two main components: Fraction.vue and App.vue. The App.vue component contains multiple instances of the Fraction component ...

Issue with ng-repeat causing problems with Bootstrap table functionality

<div ng-controller="reportCtrl"> <table class="table table-hover"> <thead class="row col-md-3"> <tr class="row"> <th class="col-md-6">Key </th> <th cla ...

The parcel command seems to be inactive and is not registered as a valid command

After successfully installing Parcel using npm, I created a package.json file with the following content: { "name": "example", "version": "0.1.0", "source": "src/index.js", "m ...