-2

I want to make random three digit number but when I run it, it generates alphanumeric characters. I want to just get a random three digit number.

generate:

function () {
    this.newItem.st = Math.random().toString(36).substring(2, 3).toUpperCase() +
                      Math.random().toString(36).substring(2, 4).toUpperCase()
}

Scren shot

When I click generate I want to get random values of just 0-9 not A-Z.

F. Hauri
  • 58,205
  • 15
  • 105
  • 122
  • please add some examples of the wanted values. – Nina Scholz Feb 12 '19 at 17:40
  • https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range - Set the range from 100-999. In PHP (as you've tagged it), `random_int(100, 999)` (PHP 7+). – Script47 Feb 12 '19 at 17:41
  • Possible duplicate of [Generating random whole numbers in JavaScript in a specific range?](https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) – Script47 Feb 12 '19 at 17:41
  • 2
    What does this have to do with `java` and `php`? – Andreas Feb 12 '19 at 17:41
  • take 3 dijit number similar 234 923 012 038 482 .... – Souraj Shah Feb 12 '19 at 17:54
  • `this.newItem.st = ((Math.floor(Math.random()*1000+5)*10+.5)/10000).toString().split(".")[1].replace(/05$/,"");` – F. Hauri Feb 13 '19 at 18:22

1 Answers1

0

Multiply your random number by 10 to get into the one's place, use Math.floor to get an integer, and then convert to string to get a string of numbers:

const el = document.getElementById("output");
el.innerHTML = getNumber();
function getNumber() {
    let myNum = Math.floor(Math.random() * 10).toString() + Math.floor(Math.random() * 10).toString() + Math.floor(Math.random() * 10).toString();
    return  myNum;
}
dikuw
  • 1,124
  • 14
  • 21