package imagej.function.unary;

import imagej.DoubleRange;
import imagej.function.UnaryFunction;

import java.util.Random;

public class AddNoiseUnaryFunction implements UnaryFunction
{
	private boolean dataIsIntegral;
	private double min;
	private double max;
	private double range;
	private Random rnd;
	
	public AddNoiseUnaryFunction(boolean isIntegral, double min, double max, double range)
	{
		this.dataIsIntegral = isIntegral;
		this.min = min;
		this.max = max;
		this.range = range;
		this.rnd = new Random();
		this.rnd.setSeed(System.currentTimeMillis());  // TODO - this line not present in original code (by design???)
	}
	
	public double compute(double input)
	{
		double result, ran;
		boolean inRange = false;
		do {
			ran = this.rnd.nextGaussian() * this.range;
			if (this.dataIsIntegral)
				ran = Math.round(ran);
			result = input + ran;
			inRange = DoubleRange.inside(this.min, this.max, result);
		} while (!inRange);
		return result;
	}
}

@@ -228,17 +228,26 @@
  double 
-m
+typeRangeM
 in;%0A%09pri
@@ -258,17 +258,26 @@
  double 
-m
+typeRangeM
 ax;%0A%09pri
@@ -288,21 +288,22 @@
  double 
-range
+spread
 ;%0A%09priva
@@ -314,17 +314,17 @@
 andom rn
-d
+g
 ;%0A%09%0A%09pub
@@ -376,17 +376,21 @@
  double 
-m
+typeM
 in, doub
@@ -392,17 +392,21 @@
  double 
-m
+typeM
 ax, doub
@@ -408,21 +408,22 @@
  double 
-range
+spread
 )%0A%09%7B%0A%09%09t
@@ -466,57 +466,85 @@
 his.
-min = min;%0A%09%09this.max = max;%0A%09%09this.range = range
+typeRangeMin = typeMin;%0A%09%09this.typeRangeMax = typeMax;%0A%09%09this.spread = spread
 ;%0A%09%09
@@ -550,17 +550,17 @@
 %09this.rn
-d
+g
  = new R
@@ -577,17 +577,17 @@
 %09this.rn
-d
+g
 .setSeed
@@ -798,17 +798,17 @@
  this.rn
-d
+g
 .nextGau
@@ -826,13 +826,14 @@
 his.
-range
+spread
 ;%0A%09%09
@@ -951,19 +951,37 @@
 his.
-min, this.m
+typeRangeMin, this.typeRangeM
 ax, 

make variable names better