-4

I'm working on a random pin generating project with JavaScript and I want to generate random pins like 0351, 0947, 0268 using Math.random() method, where the pin will start with the 0 cipher and the zero will be displayed in the output.

How can I do that?

xKobalt
  • 1,498
  • 2
  • 13
  • 19
FIDAH
  • 19
  • Can you provide some examples of what do you expect from output and what not? – xKobalt Jul 27 '20 at 10:32
  • Thanks for the reply. Actually with Math.random(), we can generate random numbers but when there is a number like 0345 the zero isn't displayed. I want the zero to be displayed in the output. – FIDAH Jul 27 '20 at 10:35
  • Prefix the randomly generated number with zero i.e var r='0'+ your randomly generated number – The concise Jul 27 '20 at 11:00

2 Answers2

0

You can display them as strings like this:

var pin = "0";
function randompin() {
    for(var i=0;i<3;i++) {
        pin = pin + Math.floor(Math.random() * 10).toString();
    }
    console.log(pin)
}

randompin(); 

So you'll get a 4-digit string starting with zero

Ritu
  • 714
  • 6
  • 14
silliton
  • 1
  • 2
0

Can this help you?

function getRandomPin() {
  return '0' + Math.floor(Math.random() * 900 + 100);
}

console.log(getRandomPin());
console.log(getRandomPin());
console.log(getRandomPin());

The bounds of this solution go from 0100 to 0999

Instead, for the following one, I added more controls about the numbers that can be generated under 0100:

function getRandomPin() {
  var temp = Math.floor(Math.random() * 1000);
  if (temp < 100) {
    if (temp < 10) {
      return '000' + temp;
    }
    else {
      return '00' + temp;
    }
  }
  else {
    return '0' + temp;
  }
}

console.log(getRandomPin());
console.log(getRandomPin());
console.log(getRandomPin());

EDIT: The following is a solution that takes out a number that start with the 0 cipher if the previous outcome was not starting with that digit, as requested by comment:

var bool = false;

function getRandomPin() {
  if (bool) {
    var temp = Math.floor(Math.random() * 1000);
    bool = false;
  }
  else {
    var temp = Math.floor(Math.random() * 10000);
    bool = true;
  }
  
  if (temp < 10) {
    return '000' + temp;
  }
  else if (temp < 100) {
    return '00' + temp;
  }
  else if (temp < 1000) {
    return '0' + temp;
  }
  else {
    return temp;
  }
}

console.log(getRandomPin());
console.log(getRandomPin());
console.log(getRandomPin());
console.log(getRandomPin());
console.log(getRandomPin());
console.log(getRandomPin());

It's correct to advice that the last solution provides a number that start with 0 every time the previous number is at least 1000, but this doesn't prevent a 0xyz number after another 0xyz one.

xKobalt
  • 1,498
  • 2
  • 13
  • 19
  • Thank you all for giving me so helpful answers. It is really awesome to get so many quality answers within such a short time. But I want to know is there any way get these numbers starting with zero randomly? for example that 0568 will generated randomly after 4521? is this possible? – FIDAH Jul 27 '20 at 11:17
  • Well, I'm not sure to understand, but about what I got, you want to receive a number like `0568` exactly after you got a number that does not start with `0`, like `4521`, is this your target? – xKobalt Jul 27 '20 at 11:22
  • xKobalt. Exactly mate.You got my point. – FIDAH Jul 27 '20 at 11:42
  • Ok, I just modified the answer, hope this help you :) – xKobalt Jul 27 '20 at 13:00
  • xKobalt. where is the modified answer mate? – FIDAH Jul 27 '20 at 13:25
  • You can see the edit from **`EDIT:`** label – xKobalt Jul 27 '20 at 13:26
  • okay got it. Thansk alot for the answer mate. – FIDAH Jul 27 '20 at 13:27
  • Always happy to help, but don't forget to upvote and accept :) – xKobalt Jul 27 '20 at 13:28