Topic: Nice way to generate a random and unique string?

I have a table (items) that has a field 'token' that is a randomly generated string which must be unique within the table.  Currently i'm doing this:

token = random_string(20) #uses a helper
while Item.find_by_token(token) != nil
  token = random_string(20)
end

@item.update(:token => token)


But this feels a bit nasty.  This must be a common problem, does anyone know a nicer way to do it?

thanks

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Nice way to generate a random and unique string?

you won't get around the while loop, but you could move it into the helper (or a second helper that warps the "random_string" one)

Re: Nice way to generate a random and unique string?

If you have users/authentication you're probably already requiring digest/? so just use that?

Maybe something like:

require 'digest/sha1'
class Item < ActiveRecord::Bas
  def self.unique_id
    Digest::SHA1.hexdigest Time.now.to_s
  end
end

and get an item unique ID by Item.unique_id -- or mix it up with filters/etc.

Re: Nice way to generate a random and unique string?

Robche wrote:

If you have users/authentication you're probably already requiring digest/? so just use that?

Maybe something like:

require 'digest/sha1'
class Item < ActiveRecord::Bas
  def self.unique_id
    Digest::SHA1.hexdigest Time.now.to_s
  end
end

and get an item unique ID by Item.unique_id -- or mix it up with filters/etc.

Thanks Robche - i was thinking of using time somehow but i was worried that if two calls came very close together they'd generate the same string.  I've used SHA1 before in salted password encryption, but would it be guaranteed to give unique results here?

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Nice way to generate a random and unique string?

can always append/prepend a random number to Time just to be certain. I usually use an approach like this for unique numbers and haven't had any major issues.

(Also, you can skip SHA1 altogether and just use a timestamp if you just need a unique number.)

Last edited by Robche (2007-11-07 11:01:58)

Re: Nice way to generate a random and unique string?

Robche wrote:

can always append/prepend a random number to Time just to be certain. I usually use an approach like this for unique numbers and haven't had any major issues.

(Also, you can skip SHA1 altogether and just use a timestamp if you just need a unique number.)

Cool, i reckon the time (in ms since epoch) plus the 20 digit random string i'm using at the moment, combined, will do the job.  cheers

Last edited by Max Williams (2007-11-07 11:09:40)

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams

Re: Nice way to generate a random and unique string?

gem install uuid

cd /myproject/path

uuid-setup


require_gem 'uuid'

my_unique_id_var = UUID.new


http://trac.labnotes.org/cgi-bin/trac.c … dGenerator

Also the Time and SHA hashing could potentially cause a conflict if the system clock is reset or mis-set. When tracking it down to the millisecond the odds of the same time coming up twice are slim, even with a reset, but if it does come up twice the SHA hash will generate the same UUID twice which would lead to conflict.

UUID uses some combination of time, a psuedo-random source (ie /dev/random), and  a seed. The seed is generated from some reasonably unique system variables such as a processor id or a MAC address. Using a reasonably random seed with a psuedo-random source leads to a good UUID. Conflicts are still possible, but are less likely than using a hashing algorithm and since its a standard those collisions are likely to be found sooner and the gem updated to correct the mistake.

All of that and its a gem with one setup command and two lines of code that is proven to work.

So endeth the sermon. tongue

Last edited by jbartels (2007-11-07 11:50:17)

Re: Nice way to generate a random and unique string?

ahhh...now that is EXACTLY what i was after.  Many thanks!

###########################################
#If i've helped you then please recommend me at Working With Rails:
#http://www.workingwithrails.com/person/ … i-williams