Results 1 to 7 of 7
Nov 16, 2005, 18:01 #1
- Join Date
- Jan 2003
- 0 Post(s)
- 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:
# 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
To concat in Ruby you use the '+' operator, but I think there's something else wrong in what I'm trying to do.