# Help with generating random item number

**URL:** <https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432>\
**Category:** Rails\
**Created:** [September 1, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432 "2008-09-01T19:35:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/1 "2008-09-01T19:35:13Z")

</div>

Hey all,

when a user submits a new item in my app, I want to generate a random  
7-digit number for that item. So far, I am a little unclear as to how  
this should work.

Here is what I have in my item.rb model

# generate a random item number for each new listing

def generate\_item\_number  
rand(9999999).to\_s.center(7, rand(9).to\_s)  
end

Now, my question is…

Should I create this random number in my items controller right before  
it saves the item to the DB?

def create  
# associate user to new item listing  
@item = @current\_user.items.build(params[:item])  
-------------GENERATE RANDOM NUMBER HERE--------------???  
if @item.save  
flash[:notice] = ‘Your item has been successfully submitted!’  
redirect\_to items\_path  
else  
flash[:notice] = “Uh Oh! Your item cannot be posted yet.”  
render :action =\> “new”  
end  
end

I am thinking that is best method for doing this, but how would I  
actually do it? Is this the cleanest way to produce this number?

Thanks!

---

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 7:53pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/2 "2008-09-01T19:53:58Z")

</div>

On 1 Sep 2008, at 18:34, CPerry wrote:

> def generate\_item\_number  
> rand(9999999).to\_s.center(7, rand(9).to\_s)  
> end

I would say 1000000 + rand(8999999) is slightly more obvious.

> -------------GENERATE RANDOM NUMBER HERE--------------???  
> actually do it? Is this the cleanest way to produce this number?

I’d do it in a model before\_create/before\_save. This shouldn’t be the  
controller’s concern

Fred

---

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 8:27pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/3 "2008-09-01T20:27:22Z")

</div>

Thanks Frederick, I see what you area saying now.

So, in my model, I have now added

class Item \< ActiveRecord::Base  
#add associations  
belongs\_to :user

before\_save :generate\_item\_number

end

Now, how would I actually have this random number written to my  
“item\_number” column in the DB? Should I use a hidden field that calls  
that method?

–Cory

On Sep 1, 1:52 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 8:31pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/4 "2008-09-01T20:31:29Z")

</div>

I concur with Fred. Keep it in the model. If you want to make it more  
obvious and/or reusable create a public class method for this and call  
that from the before\_save/update filters.

As far as true random number generation, that is more of a political  
discussion. Using rand should be adequate. I would like to ask, is the  
number supposed to be unique across that model, because in theory it  
could occur 10X in a row and would not be truly random. If you want  
something that is a big number/pkey for that item, i would also look  
at using a hash on the item using the model primary key as salt. That  
in theory can give you a random looking number/string but in reality  
to unique like a pkey too. just a thought.

H

On Sep 1, 1:52 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 9:27pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/5 "2008-09-01T21:27:05Z")

</div>

Excellent, Thanks Frederick, it works perfectly!

@Blasterpal…

Yes, the number does truly need to be random as well as unique across  
the model as I would not want 2 items to ever have the same item  
number. Most of these items will (later on) be searched and looked up  
by this number, so obviously duplicates can’t happen.

So what you are saying is that my rand sequence I posted above could  
at some point generate the same number over again?

–Cory

On Sep 1, 2:58 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 8:58pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/6 "2008-09-01T20:58:59Z")

</div>

On Sep 1, 7:26 pm, CPerry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Now, how would I actually have this random number written to my  
> “item\_number” column in the DB? Should I use a hidden field that calls  
> that method?

No. just do all that in the before\_save, ie

def my\_before\_save  
self.item\_number = …  
end

(you might want it to be a before\_create rather than a before\_save)

Fred

---

<div class="post-metadata">

**Author:** ![CPerry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9d8465/32.png) [@CPerry](https://www.ruby-forum.com/u/CPerry)\
**Post date:** [September 1, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/help-with-generating-random-item-number/147432/7 "2008-09-01T21:34:42Z")

</div>

On Sep 1, 8:26 pm, CPerry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> at some point generate the same number over again?

Absolutely (it wouldn’t be very random if it never produced values it  
has already produced). Any individual number has the same chance of  
being produced at any given time. if you want a uniqueness guarantee  
you’ll have to enforce that yourself (and to be double sure stick a  
unique index on the relevant column).

Fred
