Forum: Ruby on Rails Creating a String of Random Characters and Numbers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John K. (Guest)
on 2006-02-14 02:37
(Received via mailing list)
I want to create a random string of length n that is made up of
characters and integers.  Is there a function in rails or ruby that
allows me to do this?

Thank you my friend :-).

John K.
http://www.kopanas.com


=====================================================================
http://www.soen.info - source of the freshest software engineering
information on the net
http://cusec.soen.info - software engineering conference
David M. (Guest)
on 2006-02-14 03:11
(Received via mailing list)
How about md5?  It's possibly not ideal, but it is very simple to use

It generates strings of (I think) 40 characters in length, when you
give it a string to encrypt.  For an (random) string to encrypt, you
could e.g. concatenate the MAC address of your LAN card with the
current date and time from Time.now.  md5 works on a one-way hash
algorithm, so it's (practically) impossible to start with the
generated md5 string and work backwards to get the string you
originally encrypted.

If 40 characters is too long, just truncate it.

Would that suit?

Sample use:
require 'md5'
md5_string = MD5.md5('random string to encrypt').hexdigest
print md5_string      # <<<--- should give you 40 pseudo-random
characters

Regards

Dave M
Gregory S. (Guest)
on 2006-02-14 03:17
(Received via mailing list)
On Mon, Feb 13, 2006 at 07:36:54PM -0500, John K. wrote:
} I want to create a random string of length n that is made up of
} characters and integers.  Is there a function in rails or ruby that
} allows me to do this?

def randStr(len)
  (1..len).inject("") { |s, x| s << rand(128) }
end

} Thank you my friend :-).
} John K.
--Greg
Pete Y. (Guest)
on 2006-02-14 03:19
(Received via mailing list)
md5 seems like overkill, and you still have to generate a random
string to give to it anyway!

Here's what I do in some of my code:

   token_chars  = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
   token_length = 10

   Array.new(token_length) { token_chars[rand
(token_chars.length)] }.join


Cheers,

Pete Y.
Gregory S. (Guest)
on 2006-02-14 03:27
(Received via mailing list)
On Mon, Feb 13, 2006 at 08:16:46PM -0500, Gregory S. wrote:
} On Mon, Feb 13, 2006 at 07:36:54PM -0500, John K. wrote:
} } I want to create a random string of length n that is made up of
} } characters and integers.  Is there a function in rails or ruby that
} } allows me to do this?
}
} def randStr(len)
}   (1..len).inject("") { |s, x| s << rand(128) }
} end

Sorry to respond to my own post, but it looks like you wanted
alphanumerics. Try this instead (I also changed the method name to
follow
Ruby conventions):

def rand_str(len, domain)
  (1..len).inject("") { |s, x| s << domain[rand(domain.length)] }
end

You can then call it as:

alphanum = [ ('a'..'z').to_a, ('A'..'Z').to_a, ('0'..'9').to_a ].flatten
rand_str(10, alphanum)

} } Thank you my friend :-).
} } John K.
--Greg
Jake J. (Guest)
on 2006-02-14 03:40
John K. wrote:
> I want to create a random string of length n that is made up of
> characters and integers.  Is there a function in rails or ruby that
> allows me to do this?
>
> Thank you my friend :-).
>
> John K.
> http://www.kopanas.com

Here's what I do to generate a "ticket" number for web access.

val = Integer('0x' + Digest::SHA1.hexdigest("#{Time.now.to_f}#{rand}"))

Get the time and salt it with a random number.  The time can be the same
between successive calls.  You can also do some fun stuff with rand to
get better randoms.  Depends on how secure you need it to be.

Then you can convert to alphanumeric using the other suggestions on this
thread.

   Jake
Rob B. (Guest)
on 2006-02-14 05:21
(Received via mailing list)
So many have already given their opinion, but here's another (mine, of
course).
This came out of an ActiveRecord model that wants to create an
attribute that is "just random enough" not to conflict with other
records.  It's meant to be typed so the letters I (eye) and O (oh)
are avoided since they might easily be mistaken for 1 (one) and 0
(zero).

It should probably not recompute the letterset each call and I've so
far avoided trying to guarantee that it will be unique among all
other records (the begin...end while() part)

It may not be what you need, but it may give you ideas.

-Rob


   def password
     read_attribute(:password) ||
       write_attribute(:password, self.class.new_password)
   end

   def self.new_password
     # generate an 8-character "password" intended to be unique
     # about 40% digits from 0..9
     # rest from letters A..H, J..N, P..Z
     digitpct = 0.4
     letterset =  ('A'..'Z').to_a - ['I', 'O']
     #begin
     passwd = ''
     8.times do
       if rand < digitpct
         passwd << rand(10).to_s
       else
         passwd << letterset[rand(letterset.length)]
       end
     end
     #end while (self.find_by_password(passwd))
     passwd
   end
This topic is locked and can not be replied to.