Generate random integer with ALL digits from 1-9

javascript

Solution

Just start with the string `123456789` and shuffle it randomly as described in How do I shuffle the characters in a string in JavaScript?

String.prototype.shuffle = function () {
    var a = this.split(""),
        n = a.length;

    for(var i = n - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
    return a.join("");
}

Problem

How do I generate a 9-digit integer that has all digits from 1-9? Like 123456798, 981234765, 342165978, etc. Doing this: ``` var min = 100000000; var max = 999999999; var num = Math.floor(Math.random() * (max - min + 1)) + min; ``` does not work give me the integer that I want most of the time (does not have ALL digits from 1 to 9). 111111119 is not acceptable because each number must have at least one "1" in it, "2", "3", ... and a "9" in it.

Original source

Related problems