Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a list is Scala with random double taking forever

I am new to Scala and am trying to get a list of random double values:

The thing is, when I try to run this, it takes way too long compared to its Java counterpart. Any ideas on why this is or a suggestion on a more efficient approach?

def random: Double = java.lang.Math.random()
var f = List(0.0)
for (i <- 1 to 200000)
 ( f = f ::: List(random*100)) 
 f = f.tail
like image 241
user659486 Avatar asked Mar 14 '11 20:03

user659486


1 Answers

You can also achieve it like this:

List.fill(200000)(math.random)

the same goes for e.g. Array ...

Array.fill(200000)(math.random)

etc ...

like image 135
Antonin Brettsnajdr Avatar answered Oct 14 '22 02:10

Antonin Brettsnajdr