Forum: Ruby on Rails Generating a random number

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.
Beb127032d16730a7f6247b809142ec6?d=identicon&s=25 command0 (Guest)
on 2008-10-17 01:39
(Received via mailing list)
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-r...

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
2505b282d57c29be797dc35b245adb4c?d=identicon&s=25 Philip Hallstrom (Guest)
on 2008-10-17 01:47
(Received via mailing list)
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
8ee257799a0f1898dd1951bd2843bc71?d=identicon&s=25 Eric Marthinsen (emarthinsen)
on 2008-10-17 01:52
(Received via mailing list)
Would something like this do the trick?

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

Regards,
Eric
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2008-10-17 04:53
(Received via mailing list)
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..
33117162fff8a9cf50544a604f60c045?d=identicon&s=25 Daniel X Moore (yahivin)
on 2008-10-17 21:03
(Received via mailing list)
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
Beb127032d16730a7f6247b809142ec6?d=identicon&s=25 command0 (Guest)
on 2008-10-28 18:19
(Received via mailing list)
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
This topic is locked and can not be replied to.