SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generating a Specified Length String of Random Numbers

    This is going to sound like a homework question, but I swear I'm not in school. So bear with me.

    Here's some pseudo-code that I wrote up.

    # I want to be able to create a random numeric string that is the length
    # specified by the user. I should take an integer as input and this should
    # correspond to the length of the string. I should generate random
    # numbers for each positional length until the string is equal to the length
    # specified by the user.

    Here's where I'm stuck:

    Code:
      # makes a new string with zero length
        n = String.new
        
        # function to return random numbers
        # don't want any zeroes and I think
        # i need to return the val as a string
        # and not an integer hence the 'to_s'
        def generateChar
          i = rand(8) + 1
          return i.to_s
        end
        
        # could be a 'while' - trying until
        # call the generateChar function
        # until the length of the new string
        # is equal to 10. length here is hardcoded
        # but eventually would be a var
        until n.length = 10
          n + generateChar()
          return n
        end
        
        # yep, generateChar() function works
        # puts generateChar()
        
        puts n
    Not sure where to go here. In PHP you can concatenate strings using the '.=' operator.

    To concat in Ruby you use the '+' operator, but I think there's something else wrong in what I'm trying to do.

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    perhaps something like
    Code:
    >> n=7
    => 7
    >> (rand(8)+1).to_s+((1..n).collect {rand(9).to_s}).join
    => "22538535"
    >> n=12
    => 12
    >> (rand(8)+1).to_s+((1..n).collect {rand(9).to_s}).join
    => "7285886160030"
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jason -

    Love your articles, by the way. And the PHP slides - the SimpleTest slides immeasurably helpful.

    Can you explain that just a little? I think I see what you're doing, but I don't really get it. There's a range in there and a new method 'collect' that I've never used. Didn't dawn on me to use 'join.'

  4. #4
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I just made the first character, then I used the range to specify how many iterate. collect took a block which returned another digit as a string. join took that resulting array and assembled it back into a string.

    Also, take this with a grain of salt, I am probably just as much of a newby at Ruby as you are.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tim Toady on the ruby track

    Code:
    len = 10
    
    s = (1..len).inject("") { |s, x| 
    	s << rand(9).to_s }
    
    # dumb and dumber but perhaps more readable
    s = ''
    len.times { s << rand(9).to_s }
    Quote Originally Posted by sweatje
    Also, take this with a grain of salt, I am probably just as much of a newby at Ruby as you are.
    Same here

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, just realized there is a bug there the range should be (1...n) to get n chars in the resulting string
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    until n.length = 10
    Should be == (in PHP too).

    .= is += in ruby. The types need to match though.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •