I make this function to get a rand name... def self.newRandName return String(1000000+rand(8999999)) end This return numbers from 0000000 to 9999999. How is the function to return letters from ''aaaaaaa'' to ''zzzzzzz'' ? Thank''s -- Pedro C. Valentini pedro-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org +55 (21) 8708-8035
Maybe there''s an easier way, but for random capital letters, I''ve used: randstring = "" for i in 1..45 randstring = randstring + (rand(26)+65).chr end On Apr 8, 2005 1:34 PM, Pedro Valentini <pedro-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org> wrote:> > I make this function to get a rand name... > def self.newRandName > return String(1000000+rand(8999999)) > end > > This return numbers from 0000000 to 9999999. > How is the function to return letters from ''aaaaaaa'' to ''zzzzzzz'' ? > > Thank''s > > -- > > Pedro C. Valentini > pedro-p14LI7ZcAE/pVLaUnt/cCQC/G2K4zDHf@public.gmane.org > +55 (21) 8708-8035 > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On Apr 8, 2005 4:10 PM, Chris Trimble <trimbo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Maybe there''s an easier way, but for random capital letters, I''ve used: > > randstring = "" > for i in 1..45 > randstring = randstring + (rand(26)+65).chr > endWell, in perl, I''d do something like this: @chars = ("a".."z") # or whatever characters you want $string .= $chars[int rand @chars] for (1 .. 45); Though I''m not entirely sure how this would be translated into ruby. -- Urban Artography http://artography.ath.cx
Pedro, Try this: def self.newRandName x='''' 7.times { x += (?a+rand(26)).chr } return x end Also, your number routine doesn''t really return 0000000-9999999, right? It returns 1000000-9999999. It might not matter for your application, but this will return what you want (a zero-padded seven digit number): def self.newRandNumber sprintf("%07d",rand(9999999)) end Andrew On Fri, Apr 08, 2005 at 05:34:01PM -0300, Pedro Valentini wrote:> > I make this function to get a rand name... > def self.newRandName > return String(1000000+rand(8999999)) > end > > This return numbers from 0000000 to 9999999. > How is the function to return letters from ''aaaaaaa'' to ''zzzzzzz'' ?
On 8 Apr 2005, at 21:34, Pedro Valentini wrote:> How is the function to return letters from ''aaaaaaa'' to ''zzzzzzz'' ?Here''s what I use to generate simple passwords: def random_password chars = [''A''..''Z'', ''a'' .. ''z'', ''0'' .. ''9''].collect{|r| r.to_a }.flatten (1..8).collect { chars[rand(chars.length)] }.join end Change the list of characters and the size of the range to suit your needs. Cheers, Alisdair