Python is beautiful
L’autre jour, j’utilisais Python pour des tests de réseaux et j’avais besoin de générer des données aléatoires pour des envois en masse. J’ai encore une fois eu la preuve que Python is beautiful et que Stackoverflow est indispensable.
Simple. Efficace. Aperçu :
>>> import string >>> import random >>> choices = string.ascii_uppercase + string.ascii_lowercase + string.digits >>> choices 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' >>> ''.join(random.choice(choices) for c in range(42)) '9fEsWeZBYx2VKwcBipnrcznOZlHoO7Bp4lVeLbYPZE' >>> ''.join(random.choice(choices) for c in range(242)) 'oYgn3yM2dUQe2kPIB3PVgZKAo7wvoJZPbUy6dCR2lWRMhe4vKl12PWpsBpoHDnWPP01ga5hn A01AzDW9GWJPwfLZZ1I3hA0fIsNkYSy2UeKETZOSZ1EnCJVl5DtJBGqmO3Gp50U1xDoIjdVQG NcQ3PIWY7XLDxUgq4r2Ikla3ccVVJWIa8elSKMkFhafcGRIQktn2pqmjMw5prj06UM6eJLSL8 xIrKYAyKQllLShAZMs1LPswH'
Salut Pierre,
D’autres langages du même genre pour le même résultat :
#Perl
@choices = (1..9,a..z,A..Z);
#Affichage ou chaine
print(join('', map{ $choices[rand(@choices)] } (1..4))."\n");
#Affichage seulement
foreach(1..4){ print $choices[rand @choices] }
#Ruby
choices = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
#Affichage ou chaine
puts((0...4).map{ choices[rand(choices.length)] }.join)
#Affichage seulement
4.times{ print choices[rand(choices.length)] }
J’aimeJ’aime
10 novembre 2013 à 9:40