Newer to Ruby


#1

Hi. I am new to Ruby and new to programming altogether, but I am really
trying to learn as fast as I can.

So here’s my problem: I need to be able to generate a random nine digit
number. The first number cannot be 9,8 or 7.

I am using Watir to test a site, but I will need to randomly generate
this number or the test will be rejected. Thank you very much for
whatever help you can give me.


#2

On Dec 7, 2005, at 2:47 PM, Kwhamec Rojas wrote:

whatever help you can give me.
See if this gives you some ideas:

digits = [rand(7)] + Array.new(8) { rand(10) }
=> [5, 6, 1, 2, 5, 4, 7, 1, 1]

James Edward G. II


#3

rand(700000000)


#4

digits = [rand(7)] + Array.new(8) { rand(10) }

=> [5, 6, 1, 2, 5, 4, 7, 1, 1]

I was thinking:

( [rand(6)+1] + Array.new(8) { rand(10) }).to_s.to_i


#5

bbazzarrakk wrote:

On Dec 7, 2005, at 2:47 PM, Kwhamec Rojas wrote:

whatever help you can give me.
See if this gives you some ideas:

digits = [rand(7)] + Array.new(8) { rand(10) }
=> [5, 6, 1, 2, 5, 4, 7, 1, 1]

James Edward G. II

Thanks a lot. This will more than do.


#6

Hi –

On Thu, 8 Dec 2005, Derek C. wrote:

On 7 Dec 2005, at 8:47pm, Kwhamec Rojas wrote:

I need to be able to generate a random nine digit
number. The first number cannot be 9,8 or 7.

rand(700000000)

That won’t always give you a nine-digit number (I’m assuming that
starting with one or more 0’s doesn’t count… though it’s not 100%
clear).

Since the useful range is 100000000 to 699999999, you could do:
rand(599999999) + 100000000

David


David A. Black
removed_email_address@domain.invalid

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!


#7

heh. sorry d.black didn’t see you’d already posted it.

j.


#8

number = ( rand( 599999999 ) + 100000000 )

… should always generate a 9 digit number.

j.


#9

Just out of curiousity, what is the basic test case for this? I only
ask because an algorithm to generate 9-digit numbers that don’t begin
with 7, 8, or 9 sounds awfully useful to generate bogus US Social
Security Numbers.

I don’t want to sound paranoid or anything; it just struct me as an odd
coincidence…

-rcoder


#10

Just out of curiousity, what is the basic test case for this? I only
ask because an algorithm to generate 9-digit numbers that don’t begin
with 7, 8, or 9 sounds awfully useful to generate bogus US Social
Security Numbers.

I don’t want to sound paranoid or anything; it just struct me as an odd
coincidence…

-rcoder


#11

rcoder wrote:

Just out of curiousity, what is the basic test case for this? I only
ask because an algorithm to generate 9-digit numbers that don’t begin
with 7, 8, or 9 sounds awfully useful to generate bogus US Social
Security Numbers.

I don’t want to sound paranoid or anything; it just struct me as an odd
coincidence…

-rcoder

That is exactly what I need to use it for. The site I am supposed to
test involves financial transactions that are logged under both, email
and ss#. Generating random emails is no problem, as they are all
directed to me anyway. The problem was generating the random ss#.

We have a perl script that dips into our own database and uses existing
accounts to generate phantom accounts and test new features, but
everyone wants to move over to Ruby.

Coincidences only happen to people who don’t pay attention. :slight_smile: