Return an array of 6 random numbers between 01 and 60

Asked

Viewed 109 times

-3

I’m trying to create random numbers (01 to 60) with 6 digits

 const valor = n => {
     var add = 1, max = 12 - add;
     if ( n > max ) {
     return valor(max) + valor(n - max);
     }
     max = Math.pow(10, n+add);
     var min = max/10; 
     var number = Math.floor( Math.random() *  (max - min + 1) ) + min;
     return ("" + number).substring(add);
     }

I don’t know what to do there...

I tried to put the var number = Math.floor( Math.random() * 60 (max - min + 1) ) + min; but makes a mistake...

  • Your code is generating a string with n random digits perfectly. I’m not sure what your goal is. " 6 digits" is the number of digits you can use to generate the number? If so, which digits can be used? If not, try to exclaim the question better.

  • Random number is all right there , but I want example so when I put value(6) is random number with 6 digits right? ( 12, 92, 76, 65, 23, 99) but I don’t want to take the maximum of 60 and minimum 01 understood?

  • You want random numbers between 1 and 60, that’s it??

  • Simm , not in a single number, is with 6... (0, 0, 0, 0, 0, 0)

  • Do you want 6 numbers from 1 to 60? Like: [12, 59, 41, 20, 30, 44]?

  • Yeah, that’s what I want!

  • 1

    Just remembering that the answer below can generate repeated numbers (it was not clear if it could have repeated or not, but anyway, it is alert). If you want to ensure that there is no repetition, see here: https://answall.com/a/10284/112052

Show 2 more comments

1 answer

1


The function numeroAleatorio below generates a random number between min and max. The generating functionNumerosEntre1a60 will generate an array with n random numbers from the range of [1.60].

const numeroAleatorio = (min, max) => {
    return Math.floor( Math.random() *  (max - min + 1) ) + min;
};

const gerarNumerosEntre1a60 = n => {
    const resultado = [];
    for (let i = 0; i < n; ++i) {
       resultado.push(numeroAleatorio(1,60));
    }
    return resultado;
}

// Para gerar 6 números:
console.log(gerarNumerosDe1a60(6));

  • Thank you very much beast!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.