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.