Your generation of random numbers may give duplicates ... is this normal? Here you can easily generate 10 unique numbers 1-100 (by creating a random shuffled sequence of 1-100 and taking the first ten):
val r = scala.util.Random.shuffle(1 to 100).toList.take(10)
Now you can simply split the range 1-100 into those contained in your randomly generated list and those not:
val (listOfA, listOfB) = (1 to 100).partition(r.contains(_))
Now do whatever you want with these two lists, for example:
println(listOfA.mkString(",")) println(listOfB.mkString(","))
Of course, you can always just iterate over the list one by one:
(1 to 100).map { case i if (r.contains(i)) => println("yes: " + i) // or whatever case i => println("no: " + i) }
What you find simple for the loop is actually not one. This is for understanding, and it is syntactic sugar that deacupacks into a chain of call cards, flat cards and filters. Yes, it can be used in the same way as if you used the classic for-loop, but this is only because List is actually a monad. Without going into too much detail, if you want to do something in the Scala idiomatic way (the “functional” way), you should avoid trying to write classic iteratives for loops and prefer to get a collection of your data and then match their elements to accomplish what you need. Please note: collections have a truly rich library that allows you to use cool methods like partition .
EDIT (for completeness):
In addition, you should avoid side effects or at least push them as far as possible along the road. I am talking about the second example from my answer. Let's say you really need to record this stuff (you would use a logger, but println is good enough for this example). Doing it so bad. By the way, you can use foreach instead of map in this case, because you are not collecting the results, just doing side effects.
A good way would be to compute the required materials by changing each element to the appropriate line. So, calculate the necessary lines and copy them into results :
val results = (1 to 100).map { case i if (r.contains(i)) => ("yes: " + i) // or whatever case i => ("no: " + i) } // do whatever with results, eg print them
Now, results contains a list of one hundred lines of “yes x” and “no x”, but you haven’t done the ugly thing and are logging as a side effect in the matching process. Instead, you matched each item in the collection to the appropriate line (note that the original collection remains intact, so if (1 to 100) was stored in some value, it is still there; the mapping creates a new collection), and now you can do all you want with him, for example, hand him over to the registrar. Yes, at some point you need to do an “ugly side effect” and register the material, but at least you will have a special piece of code for this and you will not mix it with your display logic, which checks if it is contained in a random sequence .