This is a verification code generator, which generates n-digit numeric strings. (code can be 0000
, so I chose String
as a return type rather than Int
or Long
.)
It'll be great if anyone review this code and suggest more elegant or scalaish solution.
import scala.util.Random
object VerificationCodeGenerator {
val rand = new Random
def generate(digit: Int): String = {
val sb = new StringBuilder
for (i <- 1 to digit) {
sb.append(rand.nextInt(10))
}
sb.toString()
}
}