Random strings

I would like to know how to obtain random string of 8 characters, and if
it possible, obtaining readable strings. For example, I would like to
obtain strings like “chocoles” and not string like “akgudyfn”

John S. wrote:

I would like to know how to obtain random string of 8 characters, and if
it possible, obtaining readable strings. For example, I would like to
obtain strings like “chocoles” and not string like “akgudyfn”

As for a purely random string, I’ve got this in my lib directory…

class String
class << self
def random(opts = {})
if opts[:character_set]
chars = opts[:character_set]
else
chars = (“a”…“z”).to_a unless opts[:lowercase] == false
chars += (“A”…“Z”).to_a if opts[:uppercase]
chars += (“0”…“9”).to_a if opts[:alphanumeric]
chars += opts[:additional_character_set] if
opts[:additional_character_set]
end
newstr = “”
1.upto(opts[:length] || 42) { |i| newstr <<
chars[rand(chars.size-1)] }
return newstr
end
end
end

which lets me call

x = String.random(:length => 42)


http://www.5valleys.com/
http://www.workingwithrails.com/person/8078

John S. wrote:

I would like to know how to obtain random string of 8 characters, and if
it possible, obtaining readable strings. For example, I would like to
obtain strings like “chocoles” and not string like “akgudyfn”

As soon as you try to accomplish readability you sacrifice a lot of
randomness. If you’re willing to make that compromise, would this work?

Define two arrays, one of vowels and one of consonants. Then generate
8-letter words by choosing a random consonant, then a vowel, then… [do
4.times]. That would give you words like:

jerotowe
bunurito

…and so on.

You could try to add double-consonant sounds like ch, th, sh, but that
would make your letter count more tricky.

  • Aaron

Hi –

On Fri, 14 Mar 2008, Aaron Brown wrote:

8-letter words by choosing a random consonant, then a vowel, then… [do
4.times]. That would give you words like:

jerotowe
bunurito

You’d better trademark that last one before Taco Bell gets hold of it
:slight_smile:

David


Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.rubypal.com for details. Berlin dates coming soon!

On Mar 14, 2008, at 8:24 PM, John S. wrote:

I would like to know how to obtain random string of 8 characters,
and if
it possible, obtaining readable strings. For example, I would like to
obtain strings like “chocoles” and not string like “akgudyfn”

Take a look at Bubble Babble or RFC 1751:

http://en.wikipedia.org/wiki/Bubble_Babble
http://www.faqs.org/rfcs/rfc1751.html

Watch out for, errr, unexpected results though:

http://blog.extracheese.org/2007/12/human-readable-encryption-keys.html


PA.
http://alt.textdrive.com/nanoki/

On Mar 14, 7:24 pm, John S. [email protected]
wrote:

I would like to know how to obtain random string of 8 characters, and if
it possible, obtaining readable strings. For example, I would like to
obtain strings like “chocoles” and not string like “akgudyfn”

In addition to the other suggestions, apg (http://www.adel.nursat.kz/
apg/) is worth a look.

Fred

On Mar 14, 2008, at 8:24 PM, John S. wrote:

I would like to know how to obtain random string of 8 characters,
and if
it possible, obtaining readable strings. For example, I would like to
obtain strings like “chocoles” and not string like “akgudyfn”

Also, if you like overkill, ‘yould’ is kind of fun:

http://ygingras.net/yould


http://alt.textdrive.com/nanoki/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs