Possible combinations of (A to Z) and (0 to 9)

Asked

Viewed 2,435 times

1

Good afternoon, How many combinations can I get using (A to Z) and (0 to 9), using only 3 letters and 3 numbers, thus becoming:

AAA-000

And what’s the safest way to generate those numbers and letters?

Asim ta valendo?

$a = substr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0 ,3);
$b = substr("0123456789", 0 ,3);

echo $a ."-". $b;

Thank you.

  • @Andersoncarloswoss had an answer from you or Jefferson that talked about using Python’s libiter tools, this is a good start

  • 1

    Dear Tiago is javascript or PHP? Because the suggestions I have in mind are very different.

  • 1

    Letters will always be uppercase or can differentiate between uppercase and lowercase?

  • 26 3 x 10 3 possible combinations, simply because the positions are immutable. What is the purpose of calculating this?

1 answer

4

It works roughly as below. I will try to explain in the simplest way I know:

Total letters: 26 (taking into account only the capital letters of the common alphabet)

Total numbers: 10 (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

The bill is basically: 26 * 26 * 26 * 10 * 10 * 10 (AAA-000)

The result is:

var totalLetras = 26;
var totalNumeros = 10;

// baseado na string 'AAA-000'
var totalCombinacoes = totalLetras * totalLetras * totalLetras * totalNumeros * totalNumeros * totalNumeros;

console.log(totalCombinacoes);

A possible formula would be:

function gerarCombinacao() {
  
  var combinacao = retornarLetra().toString();
  combinacao = combinacao.concat(retornarLetra());
  combinacao = combinacao.concat(retornarLetra());
  combinacao = combinacao.concat("-");
  combinacao = combinacao.concat(retornarNumero());
  combinacao = combinacao.concat(retornarNumero());
  combinacao = combinacao.concat(retornarNumero());
  
  return combinacao;
  
}

console.log(gerarCombinacao());

//retorna as letras do alfabeto a partir do charCode: 65 até 90 (A-Z)
function retornarLetra() {
  return String.fromCharCode(64 + Math.ceil(Math.random() * 26)).toString();
}

// retorna os números de 0 a 9
function retornarNumero() {
  return Math.round((Math.random() * 9)).toString();
}

In php:

<?php

function gerarCombinacao() {

    // retorna as letras de A-Z
    $combinacao = chr(rand(65,90));
    $combinacao .= chr(rand(65,90));
    $combinacao .= chr(rand(65,90));

    // adiciona o "-"
    $combinacao .= "-";

    // retorna os números de 0 a 9;
    $combinacao .= rand(0,9);
    $combinacao .= rand(0,9);
    $combinacao .= rand(0,9);

    return $combinacao;

}

echo gerarCombinacao();

You can see this code functioning here.

  • Negative: maybe it’s because the requested code is in php. I edited the question and added the code at the end. If for any other reason, please let me know!

  • 1

    It’s complete until it’s edited.

Browser other questions tagged

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