-3

I'm trying create new random List based on other List. But this code doesn't work to update the var

import scala.util.Random
import scala.math

val randSymbol = List(1,2,2,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,6,6,7,7,7,7,7,7,8)
val _finalSymbol = List(List(0,0,0,0,0),List(0,0,0,0,0),List(0,0,0,0,0))
    var i:Int = 0
    var a:Int = 0
    for (i <- 0 to 2){
      _finalSymbol(i) = new List
      for (a <- 0 to 4){
        var iRandIndex = floor(Random.nextInt() * randSymbol.length).toInt
        var iRandSymbol = randSymbol(iRandIndex)
        _finalSymbol(i)(a) = iRandSymbol
      }
    }

1 Answers1

3

Try something like this:

val _finalSymbol = (0 to 2) map { _ => Random.shuffle(randSymbol).take(5) }

And do yourself a favor: buy a scala book. It's not javascript. At all.

Dima
  • 39,570
  • 6
  • 44
  • 70
  • Sure I will buy them, I've tried this, but this one is indexed sequence, and how do I convert this to List[List[Int]] ? – Brandy Geeks Oct 28 '16 at 15:05
  • So this works for me: `List(_finalSymbol(0),_finalSymbol(1),_finalSymbol(2))`, but can I do something very simple? – Brandy Geeks Oct 28 '16 at 15:10
  • try `_finalSymbol.toList` – Tzach Zohar Oct 28 '16 at 15:13
  • 1
    Why do you care if it is a list or not? You should not. It doesn't matter for most cases you care about. If you want it to be a list for some reason, add `.toList` at the end as @TzachZohar suggested, or replace `(0 to 2)` with `List(1,2,3)` ... But that's just a waste of ticks. You don't need it. – Dima Oct 28 '16 at 15:18