I am using this method to generate a random token in Scala:
def randomString(alphabet: String)(n: Int): String =
Stream.continually(Random.nextInt(alphabet.size)).map(alphabet).take(n).mkString
I use this method to generate a default value for a form:
val userForm = Form(
mapping(
"token" -> default(text, (randomString("0123456789abcdef")(40))),
"username" -> optional(text),
"email" -> email,
"password" -> nonEmptyText,
"gender" -> nonEmptyText
)(User.apply)(User.unapply)
)
Why do I always get the same token when running this code?
randomStringas written, it generates random tokens. Have you tried it in isolation?userForm? Is so then that's your problem.defaulttakes(Mapping[A],A)not(Mapping[A],=>A), so you only generate one token and then use it every time. I've put this as an answer, but this might not be your problem.