How to generate a list of random numbers?

后端 未结 3 584
青春惊慌失措
青春惊慌失措 2020-12-28 11:27

This might be the least important Scala question ever, but it\'s bothering me. How would I generate a list of n random number. What I have so far:

def n_ra         


        
3条回答
  •  礼貌的吻别
    2020-12-28 12:07

    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.

提交回复
热议问题