Jousi
Jousi

Reputation: 476

Generating a strings of random characters from an array and enabling repetition of randomly generated numbers

Good evening everyone,

I am trying to generate a string of 8 characters, which are randomly chosen from an array. This is what I am using:

var myArrayPismo = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; 

while(myArrayPismo.length < 9){
    var randomletter = Math.ceil(Math.random()*myArrayPismo.length)
    if(myArrayPismo.indexOf(randomletter) > -1) continue;
    myArrayPismo[myArrayPismo.length] = randomletter;
}

Which just prints out all of the letters, for some reason.

And this is my number generating function:

var kodCisla = [];
while(kodCisla.length < 9){
    var randomnumber = Math.ceil(Math.random()*9)
    if(kodCisla.indexOf(randomnumber) > -1) continue;
    kodCisla[kodCisla.length] = randomnumber;
}

Which is working fine. Except I want it to be able to generate 2 or more same numbers, not just different each time.

My goal is to get a random string of letters like this: KODlkSmQW and a random string of numbers that can also repeat like this: 887562327

Any help on either of these problems would be appreciated.

Upvotes: 0

Views: 2038

Answers (2)

gyre
gyre

Reputation: 16777

A function that can pluck a random element from an array would be useful here. Notice the use of Math.floor rather than Math.ceil, and the array access that comes before the return statement.

function randomElement (array) {
    return array[Math.floor(Math.random() * array.length)]
}

var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''),
    result = '';
for (var i = 0; i < 8; i++) {
    result += randomElement(characters)
}
console.log(result) //=> (random 8-character string)
 

For random numbers, you can use a similar randomInRange function:

function randomInRange (a, b) {
    a |= 0
    b |= 0
    return Math.floor(Math.random() * (b - a + 1)) + a
}

var result = []
for (var i = 0; i < 8; i++) {
    result.push(randomInRange(0, 9))
}
console.log(result) //=> (eight random digits)

Upvotes: 1

Scott Marcus
Scott Marcus

Reputation: 65806

Your first while loop is never running because it is based on the condition that myArrayPismo.length < 9, which is already false because you just set it to contain all 26 letters. So, when you go to look at it later, you get all the original values. What you need is an additional array to put the generated randoms into.

Also, stay away from a while loop in this case because you know exactly how many times you want to loop, so use a regular for loop instead.

Your second array (with numbers) does this and can certainly come up with the same random twice in a row.

But, both arrays don't need to do any if/then checking before pushing the random into the array.

Finally, as I mentioned, don't use Math.ceil because you'll never get the first element back as a random, use Math.floor instead.

var myArrayPismo = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; 

var resultArray = [];

for(var i = 0; i < 8; ++i){
  resultArray.push(myArrayPismo[Math.floor(Math.random()*myArrayPismo.length)]);
}

console.log(resultArray.join(""));

Upvotes: 0

Related Questions