Skip to content

Instantly share code, notes, and snippets.

@uqmessias
Created April 6, 2016 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uqmessias/159624b24a147ba3f5763bbcd9e0aa9e to your computer and use it in GitHub Desktop.
Save uqmessias/159624b24a147ba3f5763bbcd9e0aa9e to your computer and use it in GitHub Desktop.
public string GeraSenhaAleatoria(int tamanhoSenha)
{
char[] possiveisCaracteres = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$&".ToArray();
// criar um IEnumerable<bool> com o total de itens definido tamanhoSenha
string senha = Enumerable.Repeat(true, tamanhoSenha)
// Seleciona, randomicamente, alguns caracteres e retorna um IEnumerable<char> com caracteres aleatórios
.Select(c => possiveisCaracteres[rnd.Next(possiveisCaracteres.Length)])
// Junta todos os caracteres em uma única string
.Aggregate(String.Empty, (current, next) => current.ToString() + next.ToString());
return senha;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment