n-digit Verification Code Generator
if your number of digits is always going to be less than about 10, then you can use a single random operation and a string format, to do all the work without the loop.

Consider a method like:

def generate(digit: Int): String = {
    var randVal = rand.nextInt(math.pow(10, digit).toInt)
    var fmt = "%0" + digit + "d"
    fmt.format(randVal)
}


The randVal pulls a value with the limited number of digits (perhaps fewer than the limit). The format operation 0-pads the value to the right number, though.

This is not so much a scala way of doing it, but it is closer, and probably more efficient.

I would consider creating instances to handle each length of digits to avoid having to create the format each time, though.

See it running in ideone