# Perform KS-test on Random Number Generator [closed]

I've just started practicing Functional Programming in Scala in some days. This is the code I used to perform KS-test on a random number generator.

class RNG {
def next(): Double;
}

import scala.math.sqrt;
object main {
val length: Int = 100000;
def main(args: Array[String]) {
// generate 100000 random numbers
var seed: Array[Long] = Array.range(1, 7).map(x => x.toLong);
var gen: RNG = new RNG(seed);
var data: Array[Double] = (new Array[Double](length)).map {case (x) => gen.next()};

// sort data
val sorted = data.sortWith((x, y) => x < y);

// perform ks test
val baseline: Array[Int] = Array.range(0, length);
val baselinemin: Array[Double] = baseline.map {case (x) => x.toDouble/length}
val baselinemax: Array[Double] = baseline.map {case (x) => (1+x.toDouble)/length}

val diffmin = (sorted.zip(baselinemin)).map {case (x, y) => x - y};
val diffmax = (sorted.zip(baselinemax)).map {case (x, y) => y - x};

val max1 = diffmin.max;
val max2 = diffmax.max;

val d = if (max1 > max2) max1 else max2;
println("D\t" + d);
val dbase = 2/sqrt(length);
println("D base\t" + dbase);
}
}


There is a little weird thing that in line

var data: Array[Double] = (new Array[Double](length)).map {case (x) => gen.next()};


Question:

• if I remove new, the program returns a wrong answer.
• Is there any way to make this code looks more professional?

Thanks!

## closed as off-topic by yuri, Daniel, esote, Mast, GraipherJun 19 at 15:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

• Please be more specific in what you are looking for in a review. What do you mean with "looks more professional?". Also, do you understand what is happening with that map function? codereview.stackexchange.com/help/on-topic – dfhwze Jun 10 at 9:46
• This isn't a question: "if I remove new, the program returns a wrong answer." That's a statement (and probably means you shouldn't remove new). – Toby Speight Jun 10 at 11:44
• @TobySpeight I think it should be some kind of error comes out due to undefined behavior, right? – Ngọc Khánh Nguyễn Jun 12 at 3:41
• @dfhwze I'm practicing Scala, It should be some kinda standard. – Ngọc Khánh Nguyễn Jun 12 at 3:43

• All the semicolons ; can be removed. You almost never see them in idiomatic Scala.
• Unnecessary use of var. Use val.
• Replace Array.range(1, 7).map(x => x.toLong) with Array.iterate(1L, 7)(_+1).
• Replace case (x) with underscore _ when x is unused.
• Replace case (x) => x with underscore _ when x is used immediately and once.
• data.sortWith((x, y) => x < y) same thing as data.sorted.
• Variable data not needed: val sorted : Array[Double] = Array.fill(length)(gen.next()).sorted
• Unnecessary parentheses when defining diffmin and diffmax.
• Variables max1 and max2 not needed: val d = diffmin.max max diffmax.max