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.

3
  • 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? Commented Aug 2, 2020 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. Commented Aug 2, 2020 at 12:03
  • 👆or similar like this Commented Aug 2, 2020 at 12:04

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

3 Comments

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.
oh yes. I have corrected it. Thanks for pointing out.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.