# Random Integer for Text Field

**URL:** <https://www.ruby-forum.com/t/random-integer-for-text-field/75389>\
**Category:** Rails\
**Created:** [November 9, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389 "2006-11-09T16:44:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [November 9, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389/1 "2006-11-09T16:44:50Z")

</div>

Hello, I would like to populate an empty text field with a 5 digit  
random integer upon an ajax request. I am new to programming and this is  
all I have so far:

rand()

It will be used for my \_form.rhtml partial (a persons 5 digit ID number)  
so I’m wondering if something like this would belong in a helper, model,  
or view?  
Thanks for any help!

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [November 9, 2006, 5:41pm UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389/2 "2006-11-09T17:41:24Z")

</div>

> Hello, I would like to populate an empty text field with a 5 digit  
> random integer upon an ajax request. I am new to programming and this is  
> all I have so far:
> 
> rand()
> 
> It will be used for my \_form.rhtml partial (a persons 5 digit ID number)  
> so I’m wondering if something like this would belong in a helper, model,  
> or view?  
> Thanks for any help!

helper or model depending on the day I suppose… but I’m not sure I’d  
do  
this… just because you use rand() doesn’t mean that it’s going to be  
_unique_ and I’m guessing you’d like your users 5 digit ID numbers to be  
unique, yeah?

Just something to think about…

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [November 9, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389/3 "2006-11-09T18:41:44Z")

</div>

The ID number will mainly be used to keep track of the person’s  
attendance. The person will check in via USB numeric keypad and if the  
5 digit numbers are too close or similiar there is more room for error  
(checking in wrong person because numbers are so close). I figured if  
the numbers are random they will be spread out more from each other and  
hence less room for error. The number of people will most likely be  
under 500.  
To make each ID number unique I could use a validation for uniqueness.  
If there is a better way to do all of this please let me know. Thanks!

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [November 9, 2006, 7:59pm UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389/4 "2006-11-09T19:59:12Z")

</div>

Justin Ko wrote:

> The ID number will mainly be used to keep track of the person’s  
> attendance. The person will check in via USB numeric keypad and if the  
> 5 digit numbers are too close or similiar there is more room for error  
> (checking in wrong person because numbers are so close). I figured if  
> the numbers are random they will be spread out more from each other and  
> hence less room for error. The number of people will most likely be  
> under 500.  
> To make each ID number unique I could use a validation for uniqueness.  
> If there is a better way to do all of this please let me know. Thanks!

rand generates a random number between 0 and 1. You can give an integer  
argument to rand to define the upper limit of the numbers.

rand(1000) #=\> 297

So you can add this to your model’s initialize method. This will make  
sure that every new object has a random id.

class Foo \< ActiveRecord::Base  
def initialize  
self[:code] = rand(1000)  
end  
end

This doesn’t handle the unique problem however.

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [November 10, 2006, 7:38am UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389/5 "2006-11-10T07:38:57Z")

</div>

Okay this is heavier then I anticipated. I’m going to learn some more  
Ruby … Thanks for the suggestions!

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [November 9, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/random-integer-for-text-field/75389/6 "2006-11-09T20:42:15Z")

</div>

I would submit that you do this in the model as a called function of the  
creating/inserting function and loop around the rand until a unique id  
is found.

ps. don’t forget to seed the rand otherwise it would become vary  
inefficient.
