How to generate a list of random numbers?
Solution 1:
You can either use Don's solution or:
Seq.fill(n)(Random.nextInt)
Note that you don't need to create a new Random
object, you can use the default companion object Random, as stated above.
Solution 2:
How about:
import util.Random.nextInt
Stream.continually(nextInt(100)).take(10)
Solution 3:
regarding your EDIT,
nextInt
can take an Int
argument as an upper bound for the random number, so 1 to 20 map r.nextInt
is the same as 1 to 20 map (i => r.nextInt(i))
, rather than a more useful compilation error.
1 to 20 map (_ => r.nextInt(100))
does what you intended. But it's better to use Seq.fill
since that more accurately represents what you're doing.