Generating a random number

Would something like this do the trick?

random ={rand(6)}.join


What I did:

  def self.generate_invoice_id
    record =
    while record
      random = rand(999999999)
      record = find(:first, :conditions => ["invoice_id = ?", random])
    return random

Put that in the model and I get a random invoice number when I need

How about something like:

"%09d" % rand(1000000000)

Or... this way seems even better:{rand 10}.join

Where 9 can be replaced by however many digits you want.


Thanks everyone for your help. A couple different ways actually worked
that you all suggested.

That was just driving me crazy not being able to figure out the small
details of a random number in Rails. Every language does it slightly
differently too.