2

Given an upper bound in array - elementsGroupItemsCount, for example, which has 101 elements. How would I randomly select a minimum and maximum value from this range 0-100, where the minimum and maximum value are n numbers apart and need to contain n elements.

E.g. if the numbers need to be contain 6 elements, the following would be valid solutions:

  • 5,10
  • 12,17
  • 19,24

I've seen how to generate random numbers in a range, but not sets of random numbers that are interrelated to each other.

methuselah
  • 12,766
  • 47
  • 165
  • 315
  • so you have a bound `[lower, upper]`, and you want `n` numbers between those 2 bounds? can you please make a more formal example? like a function prototype, a sample input, and the expected output? – Alberto Sinigaglia Aug 02 '20 at 12:03
  • 2
    Oke so I'm not sure if I understand your question correctly, but can't you just generate one number and than add `n` to it to generate the second number? You'll just have to make sure that the first generated number is not to close to the end of the list. – Bob van 't Padje Aug 02 '20 at 12:03
  • or similar like [this](https://stackoverflow.com/a/1527820/863110) – Mosh Feu Aug 02 '20 at 12:04

3 Answers3

3

As per Bob's comment it looks like you just need to get a random number between 0 and the array length (less the required span) for your lower limit and then add the required span back to it for the upper limit. e.g.

let myArray = [];

for (let i = 0; i<101; i++){
  myArray.push({objectID:i})
}

let requiredSpan = 6;
let lowerBand = Math.floor(Math.random()*(myArray.length-requiredSpan));
let upperBand = lowerBand + requiredSpan-1;

console.log(lowerBand, upperBand);
Mark Taylor
  • 1,128
  • 8
  • 15
2

Just to complete the Mark Taylor's Answer here a more reusable way

let myArray = [];

for (let i = 0; i<101; i++){
  myArray.push({objectID:i})
}

const randomMaxMin = max => min => Math.floor(Math.random()*(max-min))
const lowerBand = requiredSpan => length => randomMaxMin(length)(requiredSpan);
const upperBand = requiredSpan => lowerBand => requiredSpan + lowerBand-1;

const getLowerAndUpperBand = requiredSpan => length => {
  const lower = lowerBand(requiredSpan)(length);
  return {
    lower: lower,
    upper: upperBand(requiredSpan)(lower)
  }
}

console.log(getLowerAndUpperBand(6)(myArray.length))

You can even specialize the generic function by choosing the requiredSpan

const getLowerAndUpperBandSix = getLowerAndUpperBand(6)
console.log(getLowerAndUpperBandSix(myArray.length))
jdc
  • 384
  • 4
  • 15
0
getNRandomNumbers = (elementsGroupItems,requiredSpan) => {
  var result = []
  var startingValue = elementsGroupItems[0]
  var endingValue = elementsGroupItems[elementsGroupItems.length -1]
  var initialNum = startingValue + Math.floor(Math.random()*(endingValue - requiredSpan))
  for(let i=initialNum; i< (initialNum+requiredSpan); i++){
    result.push(i)
  }
  return result
}

console.log(getNRandomNumbers([1,2,3,4],2)) //example
jdc
  • 384
  • 4
  • 15
AFLAH ALI
  • 451
  • 5
  • 17
  • Your code will crash... first -> `n` is not defined and `for (int i ...)` will throw a syntax error. Please review your code before to share it. – jdc Aug 03 '20 at 06:54
  • oh yes. I have corrected it. Thanks for pointing out. – AFLAH ALI Aug 03 '20 at 17:34
  • If you try you're actual code it won't working. You have to remove or replace `int i` -> `let i`. Because `int` is unexpected identifier. – jdc Aug 04 '20 at 10:54