<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>random utterings...: Fixed Length Random numbers in Ruby</title>
    <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Fixed Length Random numbers in Ruby</title>
      <description>&lt;p&gt;My latest project is almost complete and we're setting up a demo site with lots of fake data already included so I used the faker gem to generate most of it.&lt;/p&gt;

&lt;p&gt;One issue I had that I couldn't do with the faker gem out of the box is get a fixed length random number.  I need this for license numbers, credit card numbers, phone numbers and others.  The faker gem does offer a random phone number but the ones in my project don't support extensions or punctuation so rather than generating the number then using string functions to strip out the stuff I didn't want, I decided to find an easier way.&lt;/p&gt;

&lt;p&gt;I started by Googling for it assuming that someone else had already figured it out and while I'm sure its been done a hundred times before, I couldn't find it anywhere.  So after much thought and digging through the ruby docs, this is what I came up with.&lt;/p&gt;

&lt;blockquote&gt;
&lt;code&gt;
rand(9999999999).to_s.center(10, rand(9).to_s)
&lt;/code&gt;
&lt;/blockquote&gt;

&lt;p&gt;rand(9999999999) will give me a number between 0 and 9999999999.  Convert it to a string and use the center method to make sure its at least 10 digits long and pad the rest with a random number from 0-9.  rand(9)&lt;/p&gt;

&lt;p&gt;If you need a number of say 7 digits in length, just change the first rand to rand(9999999) and change the center method length to 7 so it looks like this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;code&gt;
rand(9999999).to_s.center(7, rand(9).to_s)
&lt;/code&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hope this is helpful to someone.&lt;/p&gt;




</description>
      <pubDate>Wed, 09 Jan 2008 11:20:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3d11eeca-de64-42c7-86ea-9f74999c2057</guid>
      <author>Chris Barnes</author>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby</link>
      <category>Ruby on Rails</category>
      <category>ruby</category>
      <category>random</category>
      <category>numbers</category>
      <category>faker</category>
      <category>gem</category>
      <trackback:ping>http://blog.randomutterings.com/articles/trackback/26</trackback:ping>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Ed Hardy Outlet</title>
      <description>Anything throughout lifestyle can be good luck. The good news is investigation is just not sizzling hot to end inside situations. Lifestyle themselves even now is still an effective hypnotherapist. They who may have a new precisely why to reside in could tolerate virtually any precisely how. Right here is the examination to discover no matter whether your current quest that is known is completed: should you be well, it isn't really. My spouse and i happen every day divided involving a new wish to help the entire world and also a wish to take advantage of the entire world. This specific can make it challenging for you to prepare the morning. I truly do certainly not bum out over one particular time involving my well being.</description>
      <pubDate>Fri, 03 Jun 2011 08:47:58 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fe9f9ab1-6109-46d1-a688-6e8f8136b456</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-6614</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Pdf search</title>
      <description>If you are testing the actual values you have a small chance of getting a test failure, but without going too crazy it works alright. Thank for info! 
</description>
      <pubDate>Wed, 08 Sep 2010 12:39:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:9abd8e66-27b5-4a95-9b23-dc4c739a3af8</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-6241</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Mobile phone accessories</title>
      <description>I really like the work that has gone into making the post. I will be sure to tell my blog buddies about your content keep up the good work. Thanks
</description>
      <pubDate>Mon, 02 Aug 2010 07:21:53 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5538fdda-4215-4201-8df4-b978b18545fd</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-6082</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by karloz</title>
      <description>It helped me, thanks! :)</description>
      <pubDate>Wed, 15 Apr 2009 13:45:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fbc27a28-2dee-4a1a-acb6-5b1747365f3e</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-4356</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Mark Thurgood</title>
      <description>Come to think of it the first rand() only returns up to 9999999998 so that should be increased by one too.</description>
      <pubDate>Wed, 25 Feb 2009 09:59:30 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3d224fe8-08ae-49e7-b580-db7c8e462cc5</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-4036</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Mark Thurgood</title>
      <description>Great tip!  Thanks!
I believe that rand(9) returns 0-8 though.
Example should be:
rand(9999999999).to_s.center(10, rand(10).to_s)</description>
      <pubDate>Wed, 25 Feb 2009 09:57:16 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a93af0d4-9135-4ebc-b594-e4fe16bb359b</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-4035</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Joe</title>
      <description>Just what i needed, thanks.</description>
      <pubDate>Tue, 05 Aug 2008 15:27:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:44b58245-f246-485e-90fd-6b23d3b51c55</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-2547</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Noah Gibbs</title>
      <description>If you want to make this a function based on length, one way to get that initial "9999999" would be the expression: ("9" * length).to_i</description>
      <pubDate>Sun, 06 Jul 2008 00:44:09 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0e078428-9ce4-4514-9a74-6d8ecc99785b</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-2415</link>
    </item>
    <item>
      <title>"Fixed Length Random numbers in Ruby" by Tinks</title>
      <description>Very nice.. Thanks for the tip. :)</description>
      <pubDate>Sat, 05 Jul 2008 01:21:25 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7417b00a-754d-4213-9ec5-4c5a3d05f1fb</guid>
      <link>http://blog.randomutterings.com/articles/2008/01/09/fixed-length-random-numbers-in-ruby#comment-2408</link>
    </item>
  </channel>
</rss>

