Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/GBM/GradientBoostingRegressionAlgorithm.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/GBM/GradientBoostingRegressionAlgorithm.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/GBM/GradientBoostingRegressionAlgorithm.cs	(revision 16092)
@@ -210,5 +210,5 @@
     protected override void Run(CancellationToken cancellationToken) {
       // Set up the algorithm
-      if (SetSeedRandomly) Seed = new System.Random().Next();
+      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
       var rand = new MersenneTwister((uint)Seed);
 
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs	(revision 16092)
@@ -186,5 +186,5 @@
     protected override void Run(CancellationToken cancellationToken) {
       // Set up the algorithm
-      if (SetSeedRandomly) Seed = new System.Random().Next();
+      if (SetSeedRandomly) Seed = Random.RandomSeedGenerator.GetSeed();
 
       // Set up the results display
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourModel.cs	(revision 16092)
@@ -130,5 +130,8 @@
           // automatic determination of weights (all features should have variance = 1)
           this.weights = this.allowedInputVariables
-            .Select(name => 1.0 / dataset.GetDoubleValues(name, rows).StandardDeviationPop())
+            .Select(name => {
+              var pop = dataset.GetDoubleValues(name, rows).StandardDeviationPop();
+              return  pop.IsAlmost(0) ? 1.0 : 1.0/pop;
+            })
             .Concat(new double[] { 1.0 }) // no scaling for target variable
             .ToArray();
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/NonlinearRegression/NonlinearRegression.cs	(revision 16092)
@@ -186,5 +186,5 @@
         qualityTable.Rows.Add(testRMSERow);
         Results.Add(new Result(qualityTable.Name, qualityTable.Name + " for all restarts", qualityTable));
-        if (SetSeedRandomly) Seed = (new System.Random()).Next();
+        if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
         var rand = new MersenneTwister((uint)Seed);
         bestSolution = CreateRegressionSolution(Problem.ProblemData, ModelStructure, Iterations, ApplyLinearScaling, rand);
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestClassification.cs	(revision 16092)
@@ -135,5 +135,5 @@
     protected override void Run(CancellationToken cancellationToken) {
       double rmsError, relClassificationError, outOfBagRmsError, outOfBagRelClassificationError;
-      if (SetSeedRandomly) Seed = new System.Random().Next();
+      if (SetSeedRandomly) Seed = Random.RandomSeedGenerator.GetSeed();
 
       var model = CreateRandomForestClassificationModel(Problem.ProblemData, NumberOfTrees, R, M, Seed, out rmsError, out relClassificationError, out outOfBagRmsError, out outOfBagRelClassificationError);
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestRegression.cs	(revision 16092)
@@ -134,5 +134,5 @@
     protected override void Run(CancellationToken cancellationToken) {
       double rmsError, avgRelError, outOfBagRmsError, outOfBagAvgRelError;
-      if (SetSeedRandomly) Seed = new System.Random().Next();
+      if (SetSeedRandomly) Seed = Random.RandomSeedGenerator.GetSeed();
       var model = CreateRandomForestRegressionModel(Problem.ProblemData, NumberOfTrees, R, M, Seed,
         out rmsError, out avgRelError, out outOfBagRmsError, out outOfBagAvgRelError);
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEAlgorithm.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEAlgorithm.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.DataAnalysis/3.4/TSNE/TSNEAlgorithm.cs	(revision 16092)
@@ -275,5 +275,5 @@
       if (wdist != null) wdist.Initialize(problemData);
       if (state == null) {
-        if (SetSeedRandomly) Seed = new System.Random().Next();
+        if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
         var random = new MersenneTwister((uint)Seed);
         var dataset = problemData.Dataset;
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs	(revision 16092)
@@ -285,5 +285,5 @@
     #region Initialization
     protected override void Initialize(CancellationToken cancellationToken) {
-      if (SetSeedRandomly) Seed = new System.Random().Next();
+      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
       random.Reset(Seed);
       gauss = new NormalDistributedRandom(random, 0, 1);
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/ParameterlessPopulationPyramid.cs	(revision 16092)
@@ -231,5 +231,5 @@
     protected override void Initialize(CancellationToken cancellationToken) {
       // Set up the algorithm
-      if (SetSeedRandomly) Seed = new System.Random().Next();
+      if (SetSeedRandomly) Seed = RandomSeedGenerator.GetSeed();
       pyramid = new List<Population>();
       seen.Clear();
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs	(revision 16092)
@@ -37,21 +37,27 @@
       string[] variableNames, double[] coefficients,
       double @const = 0) {
+
       if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException();
-      ISymbolicExpressionTree p1 = null;
+
+      // Create tree for double variables
+      ISymbolicExpressionTree tree = null;      
       if (coefficients.Length > 0) {
-        p1 = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const);
-        if (factorCoefficients.Length == 0)
-          return p1;
+        tree = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const);
+        if (factorCoefficients.Length == 0) return tree;
       }
+
+      // Create tree for string variables
+      ISymbolicExpressionTree factorTree = null;      
       if (factorCoefficients.Length > 0) {
-        var p2 = CreateTree(factors, factorCoefficients);
-        if (p1 == null) return p2;
+        factorTree = CreateTree(factors, factorCoefficients, @const);
+        if (tree == null) return factorTree;  
+      }
 
-        // combine
-        ISymbolicExpressionTreeNode add = p1.Root.GetSubtree(0).GetSubtree(0);
-        foreach (var binFactorNode in p2.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
-          add.AddSubtree(binFactorNode);
-        return p1;
-      }
+      // Combine both trees
+      ISymbolicExpressionTreeNode add = tree.Root.GetSubtree(0).GetSubtree(0);
+      foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>())
+        add.InsertSubtree(add.SubtreeCount - 1, binFactorNode);
+      return tree;
+
       throw new ArgumentException();
     }
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Dataset.cs	(revision 16092)
@@ -116,17 +116,5 @@
 
     public ModifiableDataset ToModifiable() {
-      var values = new List<IList>();
-      foreach (var v in variableNames) {
-        if (VariableHasType<double>(v)) {
-          values.Add(new List<double>((IList<double>)variableValues[v]));
-        } else if (VariableHasType<string>(v)) {
-          values.Add(new List<string>((IList<string>)variableValues[v]));
-        } else if (VariableHasType<DateTime>(v)) {
-          values.Add(new List<DateTime>((IList<DateTime>)variableValues[v]));
-        } else {
-          throw new ArgumentException("Unknown variable type.");
-        }
-      }
-      return new ModifiableDataset(variableNames, values);
+      return new ModifiableDataset(variableNames, variableNames.Select(v => variableValues[v]), true);
     }
 
@@ -141,5 +129,5 @@
     }
 
-    protected Dataset(Dataset dataset) : this(dataset.variableNames, dataset.variableValues.Values) { }
+
 
     #region Backwards compatible code, remove with 3.5
@@ -318,7 +306,8 @@
     #region IStringConvertibleMatrix Members
     [Storable]
-    protected int rows;
+    private int rows;
     public int Rows {
       get { return rows; }
+      protected set { rows = value; }
     }
     int IStringConvertibleMatrix.Rows {
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/DataAnalysisProblemData.cs	(revision 16092)
@@ -163,5 +163,5 @@
 
       var variables = dataset.VariableNames.Where(variable => dataset.VariableHasType<double>(variable) || dataset.VariableHasType<string>(variable));
-      var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x)));
+      var inputVariables = new CheckedItemList<StringValue>(variables.Select(x => new StringValue(x).AsReadOnly()));
       foreach (StringValue x in inputVariables)
         inputVariables.SetItemCheckedState(x, allowedInputVariables.Contains(x.Value));
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/ModifiableDataset.cs	(revision 16092)
@@ -47,7 +47,10 @@
     public ModifiableDataset() { }
 
-    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues) :
-      base(variableNames, variableValues, cloneValues: false) { }
-
+    public ModifiableDataset(IEnumerable<string> variableNames, IEnumerable<IList> variableValues, bool cloneValues = false) :
+      base(variableNames, variableValues, cloneValues) { }
+
+    public Dataset ToDataset() {
+      return new Dataset(variableNames, variableNames.Select(v => variableValues[v]));
+    }
     public void ReplaceRow(int row, IEnumerable<object> values) {
       var list = values.ToList();
@@ -91,5 +94,5 @@
         variableValues[variableNames[i]].Add(list[i]);
       }
-      rows++;
+      Rows++;
       OnRowsChanged();
       OnReset();
@@ -98,15 +101,26 @@
     // adds a new variable to the dataset
     public void AddVariable(string variableName, IList values) {
+      InsertVariable(variableName, Columns, values);
+    }
+
+
+    public void InsertVariable(string variableName, int position, IList values) {
       if (variableValues.ContainsKey(variableName))
         throw new ArgumentException(string.Format("Variable {0} is already present in the dataset.", variableName));
 
-      if (values == null || values.Count == 0)
-        throw new ArgumentException("Cannot add variable with no values.");
+      if (position < 0 || position > Columns)
+        throw new ArgumentException(string.Format("Incorrect position {0} specified. The position must be between 0 and {1}.", position, Columns));
+
+      if (values == null)
+        throw new ArgumentNullException("values", "Values must not be null. At least an empty list of values has to be provided.");
+
+      if (values.Count != Rows)
+        throw new ArgumentException(string.Format("{0} values are provided, but {1} rows are present in the dataset.", values.Count, Rows));
 
       if (!IsAllowedType(values))
         throw new ArgumentException(string.Format("Unsupported type {0} for variable {1}.", GetElementType(values), variableName));
 
+      variableNames.Insert(position, variableName);
       variableValues[variableName] = values;
-      variableNames.Add(variableName);
 
       OnColumnsChanged();
@@ -129,5 +143,5 @@
       foreach (var list in variableValues.Values)
         list.RemoveAt(row);
-      rows--;
+      Rows--;
       OnRowsChanged();
       OnReset();
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/HeuristicLab.Random-3.3.csproj
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/HeuristicLab.Random-3.3.csproj	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/HeuristicLab.Random-3.3.csproj	(revision 16092)
@@ -120,4 +120,5 @@
     <Compile Include="GammaDistributedRandom.cs" />
     <Compile Include="ListExtensions.cs" />
+    <Compile Include="RandomSeedGenerator.cs" />
     <Compile Include="UniformDistributedRandom.cs" />
     <Compile Include="FastRandom.cs" />
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/RandomCreator.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/RandomCreator.cs	(revision 16091)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/RandomCreator.cs	(revision 16092)
@@ -94,5 +94,5 @@
       IntValue seed = SeedParameter.ActualValue;
 
-      if (setSeedRandomly) seed.Value = new System.Random().Next();
+      if (setSeedRandomly) seed.Value = RandomSeedGenerator.GetSeed();
       IRandom random = (IRandom)RandomType.Clone();
       random.Reset(seed.Value);
Index: /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/RandomSeedGenerator.cs
===================================================================
--- /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/RandomSeedGenerator.cs	(revision 16092)
+++ /branches/2839_HiveProjectManagement/HeuristicLab.Random/3.3/RandomSeedGenerator.cs	(revision 16092)
@@ -0,0 +1,31 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+namespace HeuristicLab.Random {
+  public static class RandomSeedGenerator {
+    private static readonly System.Random rand = new System.Random();
+    private static readonly object locker = new object();
+
+    public static int GetSeed() {
+      lock (locker) return rand.Next();
+    }
+  }
+}
