I cannot figure out why the r array will not sort into ascending order. I have tried Array.sort and manually sorting the array.
import java.lang.*;
import java.lang.Object;
import java.lang.Integer;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Gaussian {
public static int seed;
public static final int n = 100;
public static void main(String argv[]) {
double r[] = new double[100];
// Initiate the seed from the current time
GregorianCalendar t = new GregorianCalendar();
int t1 = t.get(Calendar.SECOND);
int t2 = t.get(Calendar.MINUTE);
int t3 = t.get(Calendar.HOUR_OF_DAY);
int t4 = t.get(Calendar.DAY_OF_MONTH);
int t5 = t.get(Calendar.MONTH);
int t6 = t.get(Calendar.YEAR);
seed = t6 + 65*(t5+12*(t4+31*(t3+24*(t2+60*t1))));
if ((seed%2) == 0) seed = seed-1;
**************This is the section giving me trouble*****************
// Put the Gaussian random numbers in the array
for (int i=0; i<n-1; i+=1) {
r = rang();
for (int l=0; l<r.length-1; l++) {
if(r[l] < r[l+1]) {
double tempValue = r[l+1];
r[l+1] = r[l];
r[l] = tempValue;
}
}
System.out.println(r[0]);
******************Between these stars*******************************
}
}
// Method to create two Gaussian random numbers from two
// uniform random numbers in [0,1].
public static double[] rang() {
double x[] = new double[1];
double r1, r2;
r1 = - Math.log(1-ranf());
r2 = 2*Math.PI*ranf();
r1 = Math.sqrt(2*r1);
x[0] = r1*Math.cos(r2);
return x;
}
// Method to generate a uniform random number in [0,1]
// following x(i+1)=a*x(i) mod c with a=pow(7,5) and
// c=pow(2,31)-1. Here the seed is a global variable.
public static double ranf() {
final int a = 16807, c = 2147483647, q = 127773,
r = 2836;
final double cd = c;
int h = seed/q;
int l = seed%q;
int t = a*l-r*h;
if (t > 0) seed = t;
else seed = c+t;
return seed/cd;
}
}
For some reason it is giving me this output:
-0.7286443240313888
0.9205595151394262
-0.1201523471771766
-0.2955395834645375
0.5562293071303744
0.5947383124976592
-0.5190410499731951
1.1878905341959594
-0.6530738641804281
1.92941716216534
-1.55458771926982
1.011542837179014
-1.2973072313970084
-0.5115664645635027
-0.4537839981939878
-0.43386113937789456
2.1877083571592637
-0.1869725174568339
1.0427194985616417
0.7491392218512473
-0.2837863829399006
0.09204148771478798
0.08980225475596745
-1.0595943397788652
0.2493101533697332
-1.3926086961785766
0.9722238128294852
0.4490619874581054
1.4379635505387074
1.4550206564181973
-0.9754513444753741
-1.6454765651087158
0.1683214049373476
0.9981636099004854
-0.7289169766110786
1.6612385375332162
0.19025688479326378
0.0830947016802825
0.4674778575126086
-0.9077431792737849
-0.5638299547034225
0.13229202082089384
1.2429372493642745
-0.006685432080368285
2.336192098747748
-0.5450098522726261
-1.6420372037670146
0.3400579125911062
0.48689741262698993
-0.5075527810259783
1.9679760629290328
-1.9114919760463223
0.5633783650935041
0.12871665512520616
-1.8826404473732248
0.16725744941405607
1.049647212107755
0.767071049830706
0.3366261688045942
-1.726395330988362
-0.15241706234915703
-0.17645549457761323
1.098469368528642
-0.3173352964219553
-2.6584067823396675
0.4136264577634812
-1.2506808927401905
2.0462718170224186
-2.380899123430688
-1.0340941198026203
-3.223035072470854
-0.1423807157151033
-0.048464495873010084
1.4690537691472332
0.9110766995396362
-0.040683539673625924
-0.3895836309957472
-0.4793889976948361
0.007621022168540105
0.4151797552606307
1.2734508381903344
0.6398148976757589
-2.0458807284022114
0.23937728902415573
0.09380205942857836
1.331532378407905
-0.09813530948364875
0.9515533393393638
-1.5924626733327882
-1.2504131049626441
0.3674623983411812
0.8204457493547238
0.2214473639135442
0.5573901544532469
1.6349106235864332
-1.4373482822115147
0.38216369985059967
-0.6869980429363977
0.30632157455967757
Instead of sorting the numbers in ascending order.
The reason the the array is not sorting is because of r = rang();.
Initially, double r[] = new double[100]; sets r to an array of length 100. However, r is being reassigned to the result of rang(), which is an array the length of 1. This causes the inner for loop to never run.
I recommend the following changes:
// I'm assuming that you want 100 Gaussian random numbers, not 99
for (int i = 0; i < r.length; i += 1) {
// you could also change rang() to return a
// double instead of an array of double
// because the array only contains 1 double
double[] randomGaussian = rang();
r[i] = randomGaussian[0];
}
// sort the array
Arrays.sort(r);
// print out the array
for(int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}
I figured it out guys!
// Put the Gaussian random numbers in the array
double copy[] = new double[n];
for (int i=0; i<n-1; i+=1) {
r = rang();
double temp = r[0];
copy[i] = temp;
}
// Sort the array
Arrays.sort(copy);
for (int i=0; i<n-1; i++) {
System.out.println(copy[i]);
}
Gives the right output!
Related
So i am kinda new to java and i need help with a problem.
I have this code:
import java.util.Random;
public class Board
{
private int NumberOfCards;
private int NumberOfPairs;
private int[] DeckOfCards;
private int CardsRemaining;
public Board(int NumberOfPairs){
this.NumberOfCards = NumberOfCards;
this.NumberOfPairs = NumberOfPairs;
this.CardsRemaining = CardsRemaining;
DeckOfCards = new int [2*NumberOfPairs];
Random numbers = new Random();
for (int i = 0; i < NumberOfPairs; i++) {
DeckOfCards[i] = numbers.nextInt();
}
The code above is not completed and there are many classes left to be completed but the
problem is that:
Lets say that NumberOfPairs = 3
This will mean that inside the array we will have the numbers 0,1,2 with random positions and this will also mean that we will have 3 positions of the array "empty" (because the size is 2*NumberOfPairs)
What i am trying to do is for example this:
Inside the array will still be the numbers 0,1,2 but twice and with random order such as:
1,0,2,2,1,0
Does anyone have any ideas ? Thank you in advance!
Oh yes i forgot to mention that the NumberOfPairs is not certain and will be given by the user via input
Because Random is random, it won't generate sequences like that. You have to actually make a sequence if that's what you want.
There's a bit of tricky math in the indexes, you should write these out by hand to see how it works.
Integer deck = new Integer[2*NumberOfPairs];
for (int i = 0; i < NumberOfPairs; i++) {
deck[i*2] = i;
deck[i*2+1] = i;
}
Now you have a list of values that aren't random but exactly the sequence you want. Now if you want them to be in a random order you need to shuffle them, like a deck of cards.
List<Integer> deckList = new ArrayList<>( Arrays.asList( deck) );
Collections.shuffle( deckList );
int i = 0;
DeckOfCards = new int[2*NumberOfPairs];
for( Integer x : deckList )
DeckOfCards[i++] = x;
Now you have some preset values in a random order. This would be a bit less complicated if you used an ArrayList for DeckOfCards instead of an plain int array. (Code is untested.)
(For comparison, I'll write the same code with DeckOfCards as an ArrayList<Integer>.)
DeckOfCards = new ArrayList<>();
for (int i = 0; i < NumberOfPairs; i++) {
DeckOfCards.add( i );
DeckOfCards.add( i );
}
Collections.shuffle( DeckOfCards );
(One more edit: if you are actually building a deck of cards, the usual way to do it is just to assign a List the numbers 0 through 51 (52 values for each card). Then a suit is numbers 0 through 3 (space, heart, diamond, club) like this card / 13 -- that's card divided by 13 and the face of each card is card % 13 where the face value of 10 or less are their own number+1, an ace is 0, and the values of jack, queen and king are 10, 11, and 12.)
The main thought of what you want to do is to get all the numbers inside an array and then suffle them.
Firstly, you must create an array with all the numbers that you want. In your case the numbers that you want are from 0 to NumberOfPairs two times. For example if the NumberOfPairs = 3, then the numbers that you have are (0, 0, 1, 1, 2, 2). Therefore, you got this code:
import java.util.Random;
public class Board {
private int NumberOfCards;
private int NumberOfPairs;
private int[] DeckOfCards;
private int CardsRemaining;
public Board(int NumberOfPairs){
this.NumberOfPairs = NumberOfPairs;
this.NumberOfCards = NumberOfCards*2;
this.CardsRemaining = CardsRemaining;
DeckOfCards = new int [2*NumberOfPairs];
Random numbers = new Random();
for (int i = 0; i < NumberOfCards; i++) {
for (int j = 0; j < 1; j++) {
DeckOfCards[i] = i;
}
}
}
}
And finally, you must suffle the numbers. To suffle the numbers, you have to
switch every current position of the array with a random one. So, for this step your code is something like this:
import java.util.Random;
public class Board {
private int NumberOfCards;
private int NumberOfPairs;
private int[] DeckOfCards;
private int CardsRemaining;
public Board(int NumberOfPairs){
this.NumberOfPairs = NumberOfPairs;
this.NumberOfCards = NumberOfPairs*2;
this.CardsRemaining = CardsRemaining;
DeckOfCards = new int [2*NumberOfPairs];
private int temp;
private int randomPos;
Random numbers = new Random();
for (int i = 0; i < NumberOfCards; i++) {
for (int j = 0; j < 1; j++) {
DeckOfCards[i] = i;
}
}
for (int i = 0; i < NumberOfCards; i++) {
randomPos = random.nextInt(NumberOfCards);
temp = DeckOfCards[i];
DeckOfCards[i] = DeckOfCards[randomPos];
DeckOfCards[randomPos] = DeckOfCards[temp];
}
}
}
And you are done. I hope that I helped you.
I use sphere benchmark function to test evolutionary strategy code with uncorrelate one step size mutation as follow:
public class cromosome {
private double[] variable = new double[2];
private double[] stepSize = new double[2];
private double fitness=0;
}
//=========================method set fitness=========================================
public void setFitness() {
for (int i=0; i<variable.length; i++) {
fitness += variable[i]*variable[i];
}
System.out.println("fitness= " + fitness);
}
my values are in range[-10;+10] after applying mutation steps its seems that my fitness is out of range.
this my mutation method
public static cromosome Mutation(cromosome cro) {
//Mutations with no correlation with one step size
Random r = new Random();
double a = r.nextGaussian();
double lr = 1 / (Math.sqrt(cro.getVariableLenght()));
double[] newMutationStep = new double[1];
newMutationStep[0] = cro.getMutationStep(0) * (Math.exp(lr * a));
double[] newVariable = new double[2];
for (int i = 0; i < cro.getVariableLenght(); i++) {
double b = r.nextGaussian();
newVariable[i] = cro.getVariable(i) + newMutationStep[0] * b;
}
cromosome newKromosom = new cromosome(newVariable[0], newVariable[1], newMutationStep[0]);
return newKromosom;
}
after many iteration the fitness values are out of range, how can I control my fitness ?
You are using r.nextGaussian(). The spread on this variable is larger than 1. Meaning you will sometimes get values below -1 or above 1. This is probably the source of the issue.
To fix it add a validation such as:
double a = r.nextGaussian();
if(a < -1){a = -1;}else if(a > 1){a = 1;}
After this handle it in the same way you are doing now.
How would i prevent duplicating numbers from random numbers.
I need to generate 5 numbers between 1 and 9 that are each different.
I would often get same numbers like 23334, how can i prevent that?
Any help would be great!
int num2 = (int) Math.round((Math.random()*9) +1);
int num1 = (int) Math.round((Math.random()*9) +1);
int num5 = (int) Math.round((Math.random()*9) +1);
int num3 = (int) Math.round((Math.random()*9) +1);
int num4 = (int) Math.round((Math.random()*9) +1);
One option is to use shuffle algorithm (e.g. Fisher-Yates shuffle ) to generate random sequence from 1 to 9, then take first 5 numbers of the sequence
Further explanation on StackOverflow: https://stackoverflow.com/a/196065/950427
Set<Integer> set=new HashSet<>();
while (set.size()<5) {
set.add( Math.round((Math.random()*9) +1));
}
After the set is filled you have 5 unique random numbers.
UPDATE: just to illustrate Jared Burrows' comment
Create a List includes the numbers that you want (1 to 9).
Generate random number from 0 to (size of the list minus 1).
Remove one element by index from the above generated random number. And add the removed element to a array which to be returned as a results
public static void main(String[] args) {
int []answers= returnRandomNonRepeatingNumbers(5,0,9);
for(int answer: answers) {
System.out.println(answer);
}
}
public static int[] returnRandomNonRepeatingNumbers(int sizeYouWant, int poolStart, int poolEnd) {
List<Integer> pool=new ArrayList<Integer>();
for(int i=poolStart;i<=poolEnd;i++) {
pool.add(i);
}
int []answers=new int[sizeYouWant];
for(int i=0;i<sizeYouWant;i++) {
//random index to be pick and remove from pool
int randomIndex = (int) Math.round((Math.random()*(pool.size()-1)));
answers[i]=pool.remove(randomIndex);
}
return answers;
}
If the number of possible random values is small, you want to use shuffle.
List<Integer> values = IntStream.range(0, 10).boxed().collect(toList());
Collections.shuffle(values);
values = values.subList(0, 5);
If the number of possible random values is large, you want to test adding them to a Set (or the original list if small enough)
Set<Integer> valueSet = new HashSet<>();
Random rand = new Random();
while(valuesSet.size() < 5) valuesSet.add(rand.nextInt(9) + 1);
List<Integer> values = new ArrayList<>(valueSet);
Collections.shuffle(values, rand);
Note: you need to shuffle the set as it doesn't preserve order. e.g. the numbers 1,2,3 will always come out in that order with HashSet, not 3,2,1.
Floyd's subset selection algorithm is designed to do exactly what you want, and is extremely efficient even for large sets. Selecting m items from a set of n is O(m) average running time, independent of n. Here's a Java implementation.
/*
* Floyd's algorithm to chose a random subset of m integers
* from a set of n, zero-based.
*/
public static HashSet<Integer> generateMfromN(int m, int n) {
HashSet<Integer> s = new HashSet<Integer>();
for (int j = n-m; j < n; ++j) {
if(! s.add((int)((j+1) * Math.random()))) {
s.add(j);
}
}
return s;
}
One possible approach to this problem can be divide & conquer. Step of following describes the approach:
Say m is the minimum & n is the maximum, within what i wanna get x number of randoms
Choose a random p between m & n. Save it to an array of answer. decrease x by 1 as we get one answer to our problem.
Now take a q a random number between m & p-1, another r a random number between p+1 & n. Fill up the answer array with q & r decrease x 1 for q and another 1 for the r.
Now carry on this process recursively, until the lower bound (m) & higher bound (n) becomes equal or x becomes 0.
Benefit: benefit of this approach is that, in worst case, it's runtime will be O(x), where x is the number of random number required. The best case scenarion is also o(x), as i have to find at least n number of random. These two comprise average case to θ(x) complexity.
import java.util.Random;
class GenerateDistinctRandom{
static int alreadyPut = 0;
static Random rand = new Random();
public static int[] generateDistinctRandom(int howMany, int rangeMin, int rangeMax)
{
int randomNumbers[] = new int[howMany];
GenerateDistinctRandom.recursiveRandomGenerator(rangeMin, rangeMax, randomNumbers, howMany);
return randomNumbers;
}
private static void recursiveRandomGenerator(int rangeMin, int rangeMax, int[] storage ,int storageSize)
{
if(rangeMax - rangeMin <= 0 || GenerateDistinctRandom.alreadyPut == storageSize)
{
return ;
}
int randomNumber = GenerateDistinctRandom.rand.nextInt(rangeMax-rangeMin) + rangeMin;
storage[GenerateDistinctRandom.alreadyPut] = randomNumber;
GenerateDistinctRandom.alreadyPut++;
//calling the left side of the recursion
recursiveRandomGenerator(rangeMin, randomNumber - 1, storage, storageSize);
recursiveRandomGenerator(randomNumber + 1, rangeMax, storage, storageSize);
}
public static void main(String []args){
int howMany = 5;
int distinctNumber[] = GenerateDistinctRandom.generateDistinctRandom(howMany 0, 9);
for(int i = 0;i < howMany;i++)
{
System.out.println(distinctNumber[i]);
}
}
}
I suppose you would need to store the ones that have been generated into an array and compare the new random number to the list to ensure it is unique.
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int[] numbers = new int[5];
int tempNumber = 0;
for(int numberCounter = 0; numberCounter < numbers.length;)
{
tempNumber = (int) Math.round((Math.random()*9) +1);
if(!contains(numbers, tempNumber)){
numbers[numberCounter++] = tempNumber;
}
}
}
public static boolean contains(final int[] numbersArray, final int tempNumber) {
for (final int numberFromArray : numbersArray) {
if (numberFromArray == tempNumber) {
return true;
}
}
return false;
}
I notice you did not use an array in your example, so in case you do not know how to use them yet, you could also make 5 variables.
int randomNumber = 0;
int firstNumber = Math.round((Math.random()*9) +1);
int secondNumber = 0;
while(secondNumber == 0){
randomNumber = Math.round((Math.random()*9) +1)l
if(randomNumber != firstNumber){
secondNumber = randomNumber;
}
}
And you could continue making while statements like that. But if you are supposed to know about arrays, you should definitely be using one to store the numbers.
How about this?
package com.se;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TestRandom {
List<Integer> comp = new ArrayList<>();
int listSize = 20;
public void doTask() {
Random ran = new Random();
int i = 0;
while(i < listSize){
int randomNumber = ran.nextInt(80) + 1;
if(!comp.contains(randomNumber)){
comp.add(randomNumber);
i++;
}
}
for(Integer num : comp){
System.out.println(num);
}
}
public static void main(String[] args) {
TestRandom testRandom = new TestRandom();
testRandom.doTask();
}
}
My professor asked us to generate random variables between 0 and 0.5. I wrote this code:
public class Random_Number_Generator {
double randomGenerator() {
Random generator = new Random();
double num = generator.nextDouble() * (0.5 - 0);
return num;
}
}
But my professor is saying this code is generating random numbers not random variables. What could this mean?
Apparently I misread the post; the following should be read with that in mind.
In that code, num and generators are local variables. A random number (a value) is assigned to the variable called num using the Random object named by the generator variable. Finally, the value stored in the variable num is returned from the method.
In any case, generator.nextDouble() returns a value between [0,1) so to get a value between [0,0.5), just scale it by half: divide it by two or, as done, multiply it by a half.
The - 0 in the above code is silly, but "okay" because (0.5 - 0) == 0.5.
(Also, it is good to get into the practice of to creating one Random instance and re-using it .. although this issue is more obvious in .NET.)
Now, actual random variable is, as far as I know, a function that maps values to their probability. I don't think you're supposed to return a function, so I've scratched this: the closest thing to what I guess you're supposed to do:
import java.util.*;
import java.lang.*;
class RandomVar
{
TreeMap<Double, Integer> variables;
public RandomVar()
{
variables = new TreeMap<Double, Integer>();
int count = Main.RandGen.nextInt(15);
double probabilityLeft = 1.0;
for (int i = 0 ; i < count - 1; i++)
{
int toPut = Main.RandGen.nextInt(100);
while (variables.containsValue(toPut)) toPut = Main.RandGen.nextInt(100);
double prob = probabilityLeft * Main.RandGen.nextDouble();
variables.put(prob, toPut);
}
int toPut = Main.RandGen.nextInt(100);
while (variables.containsValue(toPut)) toPut = Main.RandGen.nextInt(100);
double prob = probabilityLeft;
variables.put(prob, toPut);
}
public int getValue()
{
double rand = Main.RandGen.nextDouble();
double sum = 0;
for (double prob : variables.keySet()) //keySet() is sorted ascending
{
if (prob >= rand)
return variables.get(prob);
}
return variables.get(variables.lastKey());
}
//Shows probabilities of values
public void test()
{
for (double key : variables.keySet())
System.out.println(key + " :: " + variables.get(key));
}
}
class Main
{
public static Random RandGen = new Random();
public static void main (String[] args)
{
RandomVar rv = new RandomVar();
rv.test();
System.out.println("------------------------------");
for (int i = 0; i < 40 ; i++)
System.out.print(rv.getValue() + ", ");
}
}
This is very lousy solution, basically a class which allows you to return values with a set (random) probability. I still don't know if this is what you professor wants though...
Try this code:
public static void main(String[] arg) {
System.out.print(Random());
}
public static double Random() {
double START = 0;
double END = 0.5;
Random random = new Random();
double token = RandomNumber(START, END, random);
return token;
}
public static double RandomNumber(double aStart, double aEnd, Random aRandom) {
if (aStart > aEnd) {
throw new IllegalArgumentException("Start cannot exceed End.");
}
// get the range, casting to long to avoid overflow problems
double range = aEnd - aStart;
// compute a fraction of the range, 0 <= frac < range
double fraction = (range * aRandom.nextDouble());
double randomNumber = (fraction + aStart);
return randomNumber;
}
I need a simpler problem to solve. I need to solve the parallel summation of 1000 random X values and 1000 random Y values. I am using Parallel ForkJoin framework of java. With the the usage of single compute method, it's not possible to compute the summation of X values and Y values in completely different routes.
More over I need to calculate Sum of X * Y. i.e Σxiyi BUT X values traversed by one single thread assigns a separate task, and Y is inserted into threadpool as separate task. So how is it possible to multiply both X and Y values simulatanouesly i.e X * Y ==> (X = 100, Y = 150)?
First Thread is operating on X and second on Y.
Code:
public class RegressionLineForkJoin
{
//private static final ForkJoinPool forkJoinPool = new ForkJoinPool( 2 ); // use only 2 processors
private static Random r = new Random( );
private static final int NR_OF_VALUES = 1000; // X & Y VALUES
private static final int THRESHOLD = 100; // need to calculate Threshold value.
private static class RegressionLineForkJoinTask extends RecursiveTask<Integer>
{
private int m_Start;
private int m_End;
public RegressionLineForkJoinTask(int a_start,int a_end)
{
this.m_Start = a_start;
this.m_End = a_end;
}
public Integer compute()
{
Integer Sum = 0;
if(this.m_End - this.m_Start < THRESHOLD)
{
calculateSum(this.m_Start,this.m_End);
}
else
{
int split = (this.m_Start + this.m_End)/2;
RegressionLineForkJoinTask oRegressionLineTask_1 = new RegressionLineForkJoinTask(this.m_Start , split);
RegressionLineForkJoinTask oRegressionLineTask_2 = new RegressionLineForkJoinTask( split+1 , this.m_End);
// Invoke the tasks in parallel
invokeAll(oRegressionLineTask_1,oRegressionLineTask_2);
Sum += oRegressionLineTask_1.join();
Sum += oRegressionLineTask_2.join();
//Sum
}//end of else
return Sum;
}
public static void main(String[ ] args)
{
RegressionLineForkJoinTask oRegressionLineForkJoinTask_X = new RegressionLineForkJoinTask( 0,NR_OF_VALUES );
RegressionLineForkJoinTask oRegressionLineForkJoinTask_Y = new RegressionLineForkJoinTask( 0,NR_OF_VALUES );
Integer Sum_X_Values = forkJoinPool.invoke(oRegressionLineForkJoinTask_X);
Integer Sum_Y_Values = forkJoinPool.invoke(oRegressionLineForkJoinTask_Y);
System.out.println("in main after forkjoin.invoke()");
}
private static double nextRandomFunctionValue(int a_startInex,int a_endIndex)
{
double randomValue = 0.0;
randomValue = a_startInex + ( a_endIndex - a_startInex ) * r.nextDouble( );
return randomValue;
}//end of nextRandomFunctionValue
private static double calculateSum(int a_startIndex, int a_endIndex)
{
double sumValue = 0.0;
double RandomeValue = 0.0;
for(int index = a_startIndex; index< a_endIndex; index++)
{
RandomeValue = nextRandomFunctionValue(a_startIndex,a_endIndex);
sumValue += RandomeValue;
}//end of for
return sumValue;
}
}
}
You should have your 2 int arrays xs and ys created outside your tasks and given as parameters on your tasks. So your constructor
RegressionLineForkJoinTask(int a_start,int a_end)
would be
RegressionLineForkJoinTask(int a_start,int a_end, int[] vector) with vector being xs or ys.
They would use vector that way:
public Integer compute() {
int sum = 0;
if(this.m_End - this.m_Start < THRESHOLD) {
for (int i = this.m_Start; i < this.m_End; i++)
sum += this.m_vector[i];
} /* else split task as before */
}
Then on the same basis you can have a ProductRecursiveForkJoinTask working with two vectors with this constructor:
RegressionLineForkJoinTask(int a_start,int a_end, int[] vectorA, int[] vector B)
with this compute method:
public Integer compute() {
int sum = 0;
if(this.m_End - this.m_Start < THRESHOLD) {
for (int i = this.m_Start; i < this.m_End; i++)
sum += this.m_vectorA[i] * this.m_vectorB[i];
} /* else split task as before */
}