Fixed Length Random numbers in Ruby

Posted by Chris Barnes Wed, 09 Jan 2008 11:20:00 GMT

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.

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.

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.

rand(9999999999).to_s.center(10, rand(9).to_s)

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)

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.

rand(9999999).to_s.center(7, rand(9).to_s)

Hope this is helpful to someone.

Trackbacks

Use the following link to trackback from your own site:
http://blog.randomutterings.com/articles/trackback/26

  1. Ambien not working. From Ambien.
    Canada overnight ambien. Generic ambien. Is ambien safe to take during early pregnancy.

Comments - Leave a response

  1. Avatar
    Tinks 5 months later:
    Very nice.. Thanks for the tip. :)
  2. Avatar
    Noah Gibbs 5 months later:
    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
  3. Avatar
    Joe 6 months later:
    Just what i needed, thanks.
  4. Avatar
    Mark Thurgood about 1 year later:
    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)
  5. Avatar
    Mark Thurgood about 1 year later:
    Come to think of it the first rand() only returns up to 9999999998 so that should be increased by one too.
  6. Avatar
    karloz about 1 year later:
    It helped me, thanks! :)
  7. Avatar
    Mobile phone accessories over 2 years later:
    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
  8. Avatar
    Pdf search over 2 years later:
    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!
  9. Avatar
    Ed Hardy Outlet over 3 years later:
    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.