0

Im working on javascript problem code:

function randomNumberInt() {
return Math.floor(Math.random() * (1000 - 100 + 1) + 100);
}

3 Answers3

4

You can use this function:

function genRandom() {
  const digitHundreds = Math.floor(Math.random() * 9) + 1;
  let digitTens = Math.floor(Math.random() * 9);
  if (digitTens >= digitHundreds) digitTens++;
  let digitUnits = Math.floor(Math.random() * 8);
  if (digitUnits >= digitHundreds || digitUnits >= digitTens) digitUnits++;
  if (digitUnits >= digitHundreds && digitUnits >= digitTens) digitUnits++;
  return digitHundreds * 100 + digitTens * 10 + digitUnits;
}

console.log(genRandom());

Here digiHundreds, digitTens and digitUnits are the three digits of the number to generate.

  • digiHundreds has 9 choices: 1..9 (it cannot be 0)
  • digitTens has 10 choices, but excluding digiHundreds, so we choose from 0..8 and add 1 if it is greater or equal to digiHundreds
  • digitUnits has 10 choices, but excluding digiHundreds and digitTens, so we choose from 0..7 and add 1 if is greater or equal to either digiHundreds or digitTens, and add 1 more if it is greater or equal than both.

This process guarantees that the three digits are distinct. Combining the three digits to a number is a matter of multiplying them with the correct power of 10.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • What does the code do? Why can he use that function? E: updated – Nora Apr 10 '21 at 22:46
  • See explanation. – trincot Apr 10 '21 at 22:47
  • @trincot I think its better to write a self explanatory variables instead of a, b, c – Ben.S Apr 10 '21 at 22:50
  • 1
    Yes, this function works, but it's really terrible - one letter variables, usage of let, and not so clear conditions. – Vulwsztyn Apr 10 '21 at 22:50
  • 1
    @Vulwsztyn, point taken. See update. Must say I don't see how the conditions would need to be improved -- they are essential to the algorithm. – trincot Apr 10 '21 at 22:56
  • Nice of you to accept criticism <3, I still would not use `let` (only `const`), would not reuse `Math.floor(Math.random() * 9)` 4 times, would remove `;`s, and would use spaces around `*` consistently, but it's good enough as a PoC. – Vulwsztyn Apr 10 '21 at 22:59
  • 2
    I do not use `const` when I plan to do `++` on them... I would **certainly not** remove `;` as I am explicitly opposed to relying on the "automatic semicolon insertion". I'd rather control that myself. I agree on the spacing. – trincot Apr 10 '21 at 23:03
  • Very clever, took me a while to realize this is still evenly distributed ([0, n - m] gets mapped to [0, n] excluding the previous m digits). – Jonas Wilms Apr 10 '21 at 23:24
2

Fill the array untill the length is 3 and then join.

function getRandomArbitrary(min, max) {
  return Math.floor(Math.random() * (max - min) + min) + 1;
}

function randomNumberInt() {
  const result = [];
  while (result.length !== 3) {
    let random = getRandomArbitrary(0, 9);
    if (!result.includes(random)) result.push(random);
    
    // To check if the first no is not zero
    if (result.length === 1 && random === 0) result.pop();
  }
  return parseInt( result.join("") );
}

const result = randomNumberInt();
console.log(result);
DecPK
  • 24,537
  • 6
  • 26
  • 42
  • 1
    The downside here is that the function could take more than 3 iterations before it settles for a result,... on a bad day it could take 10+ iterations, while this can be done in a fixed number of operations. – trincot Apr 10 '21 at 23:02
  • 1
    Please accept the @trincot answer. Trincot answer is more optimized as compared to mine. – DecPK Apr 10 '21 at 23:37
  • 2
    @trincot TBH I've learned alot from your answers than from any book or tutorial. Thanks for sharing your knowledge – DecPK Apr 10 '21 at 23:46
0

Decide each number separately.

First get last digit (any 0-9). Then second (any 0-9, but not first). Then the first digit (any 0-9, but not first, second, or 0).

function range(n) {
  return [...Array(n).keys()] // returns [0,1,2,...,n-1]
}


function randomFromArray(arr) {
  return arr[Math.floor(Math.random() * arr.length)]
}

function randomNumberInt() {
  const digits = range(10) // or [...Array(10).keys()] if u do not want to declare range function
  const lastDigit = randomFromArray(digits)
  
  const possibleSecondDigits = digits.filter((n) => n !== lastDigit)
  const secondDigit = randomFromArray(possibleSecondDigits)
  
  const possibleFirstDigits = possibleSecondDigits.filter((n) => n !== 0 && n !== secondDigit)
  const firstDigit = randomFromArray(possibleFirstDigits)
  
  return firstDigit * 100 + secondDigit * 10 + lastDigit
}
console.log(randomNumberInt())
console.log(randomNumberInt())
console.log(randomNumberInt())
console.log(randomNumberInt())
console.log(randomNumberInt())
Vulwsztyn
  • 2,140
  • 1
  • 12
  • 20