Forum: Ruby Newer to Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9a17d15ade2eafd89eeb50010b62ff38?d=identicon&s=25 K. Rojas (flnative)
on 2005-12-07 21:47
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.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-07 21:53
(Received via mailing list)
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 Gray II
B4a3fe24e9fb82df573ae5b38aa3b872?d=identicon&s=25 dez (Guest)
on 2005-12-07 21:53
(Received via mailing list)
rand(700000000)
9c7f2f44463d1d4890f73e8a8229dd29?d=identicon&s=25 caleb (Guest)
on 2005-12-07 22:01
(Received via mailing list)
>  >> 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
9a17d15ade2eafd89eeb50010b62ff38?d=identicon&s=25 K. Rojas (flnative)
on 2005-12-07 22:06
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 Gray II

Thanks a lot.  This will more than do.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 dblack (Guest)
on 2005-12-07 22:35
(Received via mailing list)
Hi --

On Thu, 8 Dec 2005, Derek Chesterfield 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
dblack@wobblini.net

"Ruby for Rails", forthcoming from Manning Publications, April 2006!
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 jeff.darklight (Guest)
on 2005-12-07 23:00
(Received via mailing list)
number = ( rand( 599999999 ) + 100000000 )

... should always generate a 9 digit number.

j.
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 jeff.darklight (Guest)
on 2005-12-07 23:12
(Received via mailing list)
heh. sorry d.black didn't see you'd already posted it.

j.
788622ab7e1f792ca653eeacbe5c1db3?d=identicon&s=25 rcoder (Guest)
on 2005-12-08 03:44
(Received via mailing list)
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
788622ab7e1f792ca653eeacbe5c1db3?d=identicon&s=25 rcoder (Guest)
on 2005-12-08 04:05
(Received via mailing list)
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
9a17d15ade2eafd89eeb50010b62ff38?d=identicon&s=25 K. Rojas (flnative)
on 2005-12-14 13: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.  :)
This topic is locked and can not be replied to.