Reputation: 1101
I need some test company-names, like "rnd_company_blah23haf9", "rnd_company_g356fhg57" etc.
Is it possible to do something like
import scala.util.Random
val company = s"rnd_company_${Random.alphanumeric take 10 ?????}"
provided someone can fill out ????? of course.
Upvotes: 6
Views: 14841
Reputation: 1143
Something verbose than the above answers but this one helps you to constrain the alphabet:
def randomText(textLength: Int = 10, alphabet: List[Char] = ('a' to 'd').toList) = {
(1 to textLength).toList.map { charPos =>
val randomIndex = (Math.random() * alphabet.length).floor.toInt
alphabet(randomIndex)
}.mkString("")
}
Upvotes: 0
Reputation: 24812
Use .mkString("")
to create a String
from the Stream
:
scala> val company = s"rnd_company_${Random.alphanumeric take 10 mkString}"
company: String = rnd_company_BbesF0EY1o
Upvotes: 23
Reputation: 2804
You have an example here
scala> val x = Random.alphanumeric
x: scala.collection.immutable.Stream[Char] = Stream(Q, ?)
scala> x take 10 foreach println
Q
n
m
x
S
Q
R
e
P
B
So you can try this:
import scala.util.Random
val company = s"rnd_company_${(xx take 10).mkString}"
Upvotes: 3