Generating a random number

Hey guys,

Ok, so right now I am working on generating a random number with ruby
or rails?

So far I have gathered that I somehow or another want to use the
rand() helper to so, but not sure how to do it to achieve what I am
trying to accomplish.

What I am trying to do, is have a 6 digit number randomly generated,
and then tag it to a course name, for this
training portal I am writing. This is mostly so I can locate courses
in the mysql database without typing in the exact title.

I’ve been trying to search the web for possible solutions, and I found
one that I thought would work at:
http://www.coryperry.com/2008/09/02/generating-a-random-number-with-ruby/

But it didn’t seem to work so well, as its real vague on how he is
implementing it in his app.

I was trying to use it in an action called new that goes to a page
called new.html.erb in order to create a new course record. Here is
the snippet for the new action in the courses_controller file.


def new
@course = Course.new
@random = self.rand(99999).to_s.center(5, rand(9).to_s)

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @course }
end

end


So from there on the new.html.erb page, I tried to call the random
variable at the top of page, declaring it as:

<% @random => :course_num %>

Now, for clarification, course_num is the database field I created,
that is set to the int data type.

So essentially, I am trying to have a random number generated when I
go to create a new course record, and then store that output to a
variable that would then save it to the database field.

Thanks again in advance for your help.

Justin

On Oct 16, 2008, at 4:38 PM, command0 wrote:

What I am trying to do, is have a 6 digit number randomly generated,
and then tag it to a course name, for this
training portal I am writing. This is mostly so I can locate courses
in the mysql database without typing in the exact title.

Why do you want a random number? Why not just use the primary key
integer that Rails will give you for free? You don’t have to do any
work to generate it and the only downside is that if I see /course/1 I
imagine I could try /course/2 and it would work, but if you’re not
trying to “hide” these courses that shouldn’t be an issue.

@random = self.rand(99999).to_s.center(5, rand(9).to_s)

One problem with this is that you may end up with a duplicated value
sometime down the road. So you’ll have to check for uniqueness even
when doing this.

-philip

Would something like this do the trick?

random = Array.new(6){rand(6)}.join

Regards,
Eric

What I did:

def self.generate_invoice_id
record = Object.new
while record
random = rand(999999999)
record = find(:first, :conditions => [“invoice_id = ?”, random])
end
return random
end

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

How about something like:

“%09d” % rand(1000000000)

Or… this way seems even better:

Array.new(9){rand 10}.join

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

-Dimo
http://strd6.com/blog

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.

Thanks,

Justin