Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
Cambiar el tamaño de la matriz de enteros pero conservar la suma de la matriz Javascript - Distribución de probabilidad

Creo que esto es más conceptual que nada y mi caso de uso es muy específico.

Estoy tratando de crear una matriz de probabilidades para pasar a una función RNG. La matriz de probabilidades puede variar en tamaño, por lo que no puede tener una longitud estática. Estoy creando la matriz de probabilidades en función de un solo número entero 0-100. Sé que esto suena confuso, pero tengan paciencia conmigo.

Por ejemplo.

 const range = 5 const weight = 30 const weights = new Array(10).fill(0) const middleIndex = Math.floor(weight / 10) weights[middleIndex] = 100 const add = 10 - middleIndex let counter = 2 for (let i = middleIndex + 1; i < add + middleIndex; i++) { weights[i] = 100 / counter counter += 1 } counter = 2 for (let i = middleIndex - 1; i >= 0; i--) { weights[i] = 100 / counter counter += 1 } console.log(weights) // [ 25, 33.336, 50, 100, 50, 33.3336, 25, 20, 16.668, 14.285 ]

Vea que la matriz de salida tiene la forma de longitud 10, pero necesito la misma distribución de probabilidades en la longitud 5 para pasar a mi función RNG de manera que:

 const cleanWeights = compressWeights(weights, range) // This is my required function, it should output an array of length range and share the exact same distribution of probabilties with the input weights array. const rand = weightedRandom(weights) // Returns an int from range 0 to weights.length. Likely to equal ~2 - as distribution of probability (30/100) leans to lower end of range 0, 5

Si hay un mejor enfoque en conjunto para esto, hágamelo saber, si también detecta alguna oportunidad de optimización, me encantaría escucharla.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!