Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate alphanumeric string

Tags:

random

scala

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.

like image 236
FelixHJ Avatar asked May 06 '15 08:05

FelixHJ


2 Answers

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
like image 118
Marth Avatar answered Oct 02 '22 11:10

Marth


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}"
like image 25
Carlos Vilchez Avatar answered Oct 02 '22 12:10

Carlos Vilchez