0

I have generated random set of numbers (generated number) for each individual multiplier (numbers from 2 - 10).

The code itself does generates the numbers although not as expected.

Current behaviour:

  • it renders numbers (sometimes repetetive) within an array (for example 2x4 and 2x4)
  • it renders array with different length each time
  • rendered numbers multiplication value is repetetive (if there's an object with 4x7 and 7x4 it should replace one of these sets with a new value)
  • number objects are rendered on given conditions (for example number 9 multiplier will render at least once but no more than 3 times)

Expected behaviour:

  • it renders unique set of numbers for each multiplier
  • renders the array with the same length all the time (with length === 18)
  • checks if multiplication of multiplier and generated number matches the value within the array, if so then it renders another set of numbers within (still within the conditions)

This is what I got so far

const randomNumbersGenerator = () => {
  function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min);
  }

  const number2 = getRandomInt(0, 2);
  const number3 = getRandomInt(0, 3);
  const number4 = getRandomInt(0, 3);
  const number5 = getRandomInt(0, 3);
  const number6 = getRandomInt(1, 4);
  const number7 = getRandomInt(1, 4);
  const number8 = getRandomInt(1, 4);
  const number9 = getRandomInt(1, 4);
  const number10 = getRandomInt(0, 2);
  const number11 = getRandomInt(0, 3);

  const randomNumber = () => getRandomInt(2, 12);
  let current;
  const numbersArray = [];

  for (let i = 0; i < number2; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 2,
        generated: current
      });
    }
  }

  for (let i = 0; i < number3; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 3,
        generated: current
      });
    }
  }

  for (let i = 0; i < number4; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 4,
        generated: current
      });
    }
  }

  for (let i = 0; i < number5; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 5,
        generated: current
      });
    }
  }

  for (let i = 0; i < number6; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 6,
        generated: current
      });
    }
  }

  for (let i = 0; i < number7; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 7,
        generated: current
      });
    }
  }

  for (let i = 0; i < number8; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 8,
        generated: current
      });
    }
  }

  for (let i = 0; i < number9; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 9,
        generated: current
      });
    }
  }

  for (let i = 0; i < number10; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 10,
        generated: current
      });
    }
  }

  for (let i = 0; i < number11; i += 1) {
    if (numbersArray.indexOf((current = randomNumber())) === -1) {
      numbersArray.push({
        multiplier: 11,
        generated: current
      });
    }
  }

  console.log(numbersArray);
  return numbersArray;
};
randomNumbersGenerator();

You can also check it out via the codeSandbox: https://codesandbox.io/s/upbeat-jang-coqd5?file=/src/index.js:660-672

Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • "_renders the array with the same length all the time (with length === 18)_" - you don't do anything to ensure that. Your algorithm will always generate a random number of array elements because of the individual calls to `getRandomInt()`. – Randy Casburn Nov 26 '21 at 16:42
  • "_it renders unique set of numbers for each multiplier_" - again, your code does nothing to accomplish this goal. Can you explain, in terms of the output data structure, what values must be unique? Do both the `multiplier` and the `generated` values have to be unique or just the `generated` value? – Randy Casburn Nov 26 '21 at 16:43
  • thanks for getting back @RandyCasburn. You are right, although min / max generated numbers for each multiplier is a necessary condition. Though I am not sure how this can be compromised. just the generated values have to be unique. The multiplayers are const (2,3,4,5,6,7,8,9,10). For each of the multipliers I would require from 0-3 generated values (that's why i've added getRandomInt() function. – Brew WebDev Nov 26 '21 at 17:16
  • Can you answer this question: With 10 numbers (number2 - number11), how do you propose to derive 18 unique pairs? – Randy Casburn Nov 26 '21 at 17:21
  • Sorry, it could have been a misunderstanding here. To give you the better idea of it let me just paste here a copy of sample data that could get generated. [link](https://codesandbox.io/s/elastic-sid-6binc?file=/src/index.js) None of the pairs multiplied value is the same and they've met conditions of (min & max multipliers present in the array) – Brew WebDev Nov 26 '21 at 18:04
  • OK - final question: "_checks if multiplication of multiplier and generated number matches the value within the array_" - _within the array_ - So `2 * 0 = 0` what array is that matched against? – Randy Casburn Nov 26 '21 at 20:42

1 Answers1

0

I think this is what you are looking for. I took a completely different approach.

First, I'm using a Map because it is simple to ensure uniqueness. (keys must be unique)

Note I'm using a simple string for the key to ensure they are unique (using objects there get's a bit more complicated)

The ranges array represents your 'number2, number3` etc.

The loop starts at 2 to satisfy your multiplier range (2 - 11). This requires a bit of trickery to get index cone correctly.

This generates unique pairs (as key) and the value of the map is the generated value and the multiplier multiplied together.

The size and Map are printed in the console.

const randomNumbersGenerator = () => {
  function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) + min);
  }
  const randomNumber = () => getRandomInt(2, 12);
  const multiplierMap = new Map();
  const ranges = [[0, 2], [0, 3], [0, 3], [0, 3], [0, 3], [1, 4], [1, 4], [1, 4], [1, 4], [0, 2], [0, 3]];
  while(multiplierMap.size < 17) {
    for (let i = 2; i < ranges.length+1; i++) {
      const randomInt = randomNumber();
      if(Array.from(multiplierMap.values).includes(randomInt * i)){
        --i;
      } else {
        multiplierMap.set(randomInt + " " + i, randomInt * i);
      }
      if(multiplierMap.size === 18) break;
    }
  }
  console.log('MultiplierMap size: ', multiplierMap.size);
  for(let [pair, multiple] of multiplierMap){
    console.log('Generated Multiplier: ' + pair, '||', 'Generated * Multiplier: ' + multiple);
  }
  return multiplierMap;

};
randomNumbersGenerator();
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • 1
    this is just great I see what you did here without repeating yourself like I did with multiple loops. I also have noticed that you've used .set method that iterates through the elements of a set in insertion order. Great solution. Thank you for valuable explanation too! – Brew WebDev Nov 28 '21 at 21:27
  • sorry @RandyCasburn I have one more question. The multiplication result of multiplier and generatedNumber in some cases is the same (it happens that I'd get 4x2 and 2x4). Should I run another loop inside of for of multiplierMap? I have also noticed that range does not work, it happens that it produces 5 times of number 11 multiplier even though the max integer of number 11 multiplier is lower than that and sometimes it does not produce the min amount of required multiplier numbers. – Brew WebDev Nov 29 '21 at 11:37
  • it wasn't clear that both generated number and multiplication result must be unique after you told me this "_just the generated values have to be unique_" above. As far as the count of each individual multiplier you said "_For each of the multipliers I would require from 0-3 generated values (that's why i've added getRandomInt() function._" - So I assumed your solution was satisfactory. I'll take a look a bit later on and make some changes for you. – Randy Casburn Nov 29 '21 at 14:36
  • @BrewWebDev - Looking back, this "_it happens that it produces 5 times of number 11 multiplier_" is not possible. The console is clear - the multiplier is always the number listed on the right of the two. There is never more than two of any multiplier. YOUR number generator may be producing the _GENERATED_ number that is the same as the _MULTIPLIER_, but that means you need to rethink how you generate the numbers. I think that would be a different question. I'm confident I have completely answered your original question. – Randy Casburn Nov 29 '21 at 16:54
  • I have modified the code to ensure you never get two sets of `mulitpler - generated` that duplicate. So the result will be unique. – Randy Casburn Nov 29 '21 at 16:56
  • Please consider upvoting all this work. Thanks! – Randy Casburn Nov 29 '21 at 16:56
  • upvoting your work is not going to be enough, if possible drop me a message I would like to buy you at least a cup of coffee for your time. Once again thank you for your work. I am going to look into this further as I was generating numbers and sometimes it still produces the matchin pair (f.e. 9 x 6 and 6 x 9) as well as it does not really care about min / max range value sometimes (f.e. range is set to 1 or 2 but it will produce it 3 times). Gonna leave a link if it can be helpfull for someone: [link](https://codesandbox.io/s/intelligent-river-1ub53?file=/src/index.js) – Brew WebDev Nov 29 '21 at 18:03
  • You made some subtle changes that you didn't tell me about. Your sandbox builds 25 pairs rather than the 18 you described. That would certainly change the number of times each multiplier is used. I'm sure you'll get it sorted out the way you want. There is no PM capability in SO - but I'm easy enough to find. – Randy Casburn Nov 29 '21 at 18:07