Created
September 5, 2016 18:16
-
-
Save anonymous/0c0164c38f1fa44e0af56a585d8e7868 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# coding: utf-8 | |
### Chargement des librairies | |
require 'securerandom' | |
### Définition des méthodes | |
# Retourne un mot de longueur "len" généré aléatoirement | |
def get_random_word(len) | |
alphabet = "abcdefghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
alphabet_len = alphabet.size | |
random_word = "" | |
len.times do | |
random_index = SecureRandom.random_number(alphabet_len) | |
random_word << alphabet[random_index] # Concaténation du caractère | |
end | |
return random_word # Retourner le résultat | |
end | |
#Retourne un mot aléatoire de len caractères (version récursive) | |
def get_random_word_recur(len, random_word="") | |
alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | |
alphabet_size = alphabet.size | |
if len == 0 | |
return random_word | |
else | |
random_char = alphabet[SecureRandom.random_number(alphabet_size)] | |
random_word << random_char | |
return get_random_word_recur(len - 1, random_word) | |
end | |
end | |
### Partie principale | |
# Générer 50 mots de 5 caractères | |
num_words = 50 | |
word_len = 12 | |
footer_fmt = "%s random words of length %s generated in %s seconds" | |
start_time = Time.now | |
num_words.times do |num_iter| # Nombre d'iterations | |
random_word = get_random_word(12) | |
puts "[#{num_iter + 1}] #{random_word}" | |
end | |
end_time = Time.now | |
puts footer_fmt % [num_words, word_len, end_time - start_time] | |
start_time = Time.now # Temps de départ; | |
num_words.times do |num_iter| | |
random_word = get_random_word_recur(12) | |
puts "[#{num_iter + 1}] #{random_word}" | |
end | |
finish_time = Time.now | |
puts footer_fmt % [num_words, word_len, finish_time - start_time] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment