-6

I don't like this code, it is overcomplicated and impractical, so I'm looking to simplify it.

I want it to change a var by a random amount, and I need to put at least 150 variables into this code.

//Variable list

public double price1 = 100;
public double price2 = 100;
public double price3 = 100;

public void DaysEnd(){ //Simplified version of inefficient code

var = price1;
HVariation();
price1 = newvar;

var = price2;
HVariation();
price2 = newvar;

var = price2;
MVariation();
price2 = newvar;

var = price3;
LVariation();
price3 = newvar;
}

public void Hvariation(){
    newvar = var + (var * (Random.NextDouble(0 - 0.5, 0.5)));
}
public void Mvariation(){
    newvar = var + (var * (Random.NextDouble(0 - 0.25, 0.25)));
}
public void Lvariation(){
    newvar = var + (var * (Random.NextDouble(0 - 0.1, 0.5)));
}
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
f3rn0s
  • 115
  • 1
  • 7

3 Answers3

2

This should get you started

List<double> values = new List<double> { 100, 100, 200, 500, ... };
values = values.Select(val => Hvariation(val)).ToList();
// now all values have been altered by Hvariation

...
private readonly Random _rand = new Random();

public double Hvariation(double val) {
    return val + (val * (_rand.NextDouble(-0.5, 0.5)));
}
SimpleVar
  • 14,044
  • 4
  • 38
  • 60
0

The first thing to do is find repeated code. For example:

var = price3;
LVariation(); //Different variations
price3 = newvar;

This can be turned into a method (that takes the variation as a parameter). To do this, you will also need to make a default variation that takes the min and max:

public void Variation(double min, double max){
    newvar = var + (var * (Random.NextDouble(min, max)));
}

You can then put this together to reduce code to look some thing like this:

public double UpdatePrice(double price, double min, double max)
{
    var = price;
    Variation(min, max);
    return newvar;
}

In general, if I have to copy the code more than once (or even once if the amount copied is significant), I turn the code into a method.

druidicwyrm
  • 480
  • 2
  • 11
0

You can simplify this by instead of defining three variation methods, defining a variation level and passing it into a single method. I'm not sure if you would need it to be in arrays or if you can use lists (in which case lists are preferable), but you can store your variable in an array instead of defining a variable name for each one and separate them into logical groupings as you need to. You can then apply the change/transformation to each array using LINQ. An example of this would be

    public enum VariationLevel
    {
        High,
        Medium,
        Low
    };
    public double[] HighVariancePrices =
    {
        100, 100, 100, 100, 100
    };
    public double[] MediumVariancePrices =
    {
        100, 100, 100, 100, 100
    };
    public double[] LowVariancePrices =
    {
        100, 100, 100, 100, 100
    };


    public void DaysEnd()
    {
        HighVariancePrices = HighVariancePrices.Select(price => GetVariation(price, VariationLevel.High)).ToArray();
        MediumVariancePrices = MediumVariancePrices.Select(price => GetVariation(price, VariationLevel.Medium)).ToArray();
        LowVariancePrices = LowVariancePrices.Select(price => GetVariation(price, VariationLevel.Low)).ToArray();
    }

    public double GetVariation(double value, VariationLevel variationLevel)
    {
        switch (variationLevel)
        {
            case VariationLevel.High:
                return value + (value * (Random.NextDouble(0 - 0.5, 0.5)));
            case VariationLevel.Medium:
                return value + (value * (Random.NextDouble(0 - 0.25, 0.25)));
            case VariationLevel.Low:
                return value + (value * (Random.NextDouble(0 - 0.1, 0.5)));
        }
    }

However, the code around Random.NextDouble() doesn't compile (because NextDouble doesn't take arguments) so I'm not certain what you're trying to do there, but that's outside of the scope of "how can I simplify my code?" Hope this helps some.

Peter Luu
  • 446
  • 2
  • 10