John Kopanas
2006-Feb-14 00:36 UTC
[Rails] Creating a String of Random Characters and Numbers
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 Kopanas 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 Mitchell
2006-Feb-14 01:11 UTC
[Rails] Creating a String of Random Characters and Numbers
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 On 14/02/06, John Kopanas <john@protoseinc.com> 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 Kopanas > 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Gregory Seidman
2006-Feb-14 01:17 UTC
[Rails] Creating a String of Random Characters and Numbers
On Mon, Feb 13, 2006 at 07:36:54PM -0500, John Kopanas 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 Kopanas --Greg
Pete Yandell
2006-Feb-14 01:18 UTC
[Rails] Creating a String of Random Characters and Numbers
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 Yandell
Gregory Seidman
2006-Feb-14 01:27 UTC
[Rails] Creating a String of Random Characters and Numbers
On Mon, Feb 13, 2006 at 08:16:46PM -0500, Gregory Seidman wrote: } On Mon, Feb 13, 2006 at 07:36:54PM -0500, John Kopanas 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 Kopanas --Greg
Jake Janovetz
2006-Feb-14 01:40 UTC
[Rails] Re: Creating a String of Random Characters and Numbers
John Kopanas 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 Kopanas > http://www.kopanas.comHere''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 -- Posted via http://www.ruby-forum.com/.
Rob Biedenharn
2006-Feb-14 03:21 UTC
[Rails] Creating a String of Random Characters and Numbers
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 At 2/13/2006 07:36 PM, John Kopanas 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 Kopanas >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-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060214/fef8b659/attachment.html