我可以使用其他方法而不是随机方法

时间:2017-01-24 09:17:47

标签: c# winforms livecharts

Tensorflow

从上面开始,我该如何处理它而不是让R随机,我可以将它设置为减量。喜欢它像5.0 4.4 3.2等双打而不是0到5之间的随机

4 个答案:

答案 0 :(得分:0)

在设置第一个计时器的方法中,添加另一个计时器,如下所示:

//The next code simulates data changes every 500 ms
Timer = new Timer
{
    Interval = 1000
};
Timer.Tick += TimerOnTick;
R = new Random();
Timer.Start();

//another timer
SecondTimer = new Timer() { Interval = 2000 };
SecondTimer.Enabled = true;
SecondTimer.Tick += SecondTimer_Tick;

定义一些min变量

double min = 4.9;

SecondTimer_Tick方法中,调整这些变量并生成新值。 Random.NextDouble将生成从0到1的数字(即0.887),将其添加到min var。另外,将最小值减小一些值(本例中为0.2)

private void SecondTimer_Tick(object sender, EventArgs e)
{
    min -= 0.2;

    if (min == 0)
        //stop timer or something
        SecondTimer.Stop();

    Random R = new Random();
    var value = R.NextDouble();
    value += min;
}

希望这有帮助。

修改 添加一些检查randomizer生成的值是否太大。例如,如果你的min是4.8并且生成的值是0.5 - 那么将它们加在一起就会得到5.3,我认为这对你的案例来说太大了。

答案 1 :(得分:0)

var r = R.NextDouble()*5会给出0到5之间的十进制数。

语义上您可以反转这样的数字,var r= 5 - R.NextDouble()*5

Ofcourse 5可以随任何数字变化。

现在在每个计时器滴答时从随机生成的数字递减值。

private double decrementingValue = 5;

... 

r = R.NextDouble()* 5;
decrementingValue -= r;

if(decrementingValue <= 0) 
       decrementingValue= 0; // you are finished

答案 2 :(得分:0)

您可以使用抽象测量源

public interface IMeasurementSource
{
    MeasureModel GetNext();
}

为您的类(可能是Form)提供一些IMeasurementSource实现,并将其保存在类字段中。 E.g。

private IMeasurementSource measurementSource = new BouncingMesasurementSource(5, 0.1);

每次计时器滴答时调用它:

ChartValues.Add(measurementSource.GetNext());

实施抽象

您可以通过从真实来源获取测量值来实现此界面。或者您可以使用随机测量生成器:

public class RandomMeasurementSource : IMeasurementSource
{
    private readonly Random random = new Random();
    private readonly int min;
    private readonly int max;

    public RandomMeasurementSource(int min, int max)
    {
        this.min = min;
        this.max = max;
    }

    public MeasureModel GetNext()
    {
        return new MeasureModel { DateTime = DateTime.Now, Value = random.Next(min, max) };
    }
}

或弹跳将返回并强制在零和最大值之间:

public class BouncingMeasurementSource : IMeasurementSource
{
    private readonly double max;
    private double step;
    private double current;

    public BouncingMeasurementSource(int max, double step)
    {
        this.max = max;
        this.step = step;
        this.current = max;
    }

    public MeasureModel GetNext()
    {
        var model = new MeasureModel { DateTime = DateTime.Now, Value = current };
        current -= step;

        if (current < 0 || max < current)
        {
            step = -step;
            current = current < 0 ? 0 : max;
        }

        return model;
    }
}

答案 3 :(得分:0)

构建一个生成器类,它将为您完成新值的所有计算。

作为一个例子,这里有一个非常基本的实现,可以作为你的课程的入门者

enum Direction
{
    Increase,
    Decrease,
}

class ValueGenerator
{
    private readonly Random _generator;

    public ValueGenerator( Random generator )
    {
        if ( generator == null )
            throw new ArgumentNullException( nameof( generator ) );
        _generator = generator;
    }

    public double MinInterval { get; set; } = 0;
    public double MaxInterval { get; set; } = 1;
    public double MinValue { get; set; } = 0;
    public double MaxValue { get; set; } = 100;
    public double CurrentValue { get; set; } = 0;
    public Direction CurrentDirection { get; set; } = Direction.Increase;
    public int PossibilityToChangeDirection { get; set; } = 10;

    public double NextValue()
    {
        if ( _generator.Next( PossibilityToChangeDirection + 1 ) == PossibilityToChangeDirection / 2 )
        {
            switch ( CurrentDirection )
            {
                case Direction.Increase:
                    CurrentDirection = Direction.Decrease;
                    break;
                case Direction.Decrease:
                    CurrentDirection = Direction.Increase;
                    break;
                default:
                    throw new InvalidOperationException( );
            }
        }

        var newInterval = ( MaxInterval - MinInterval ) * _generator.NextDouble( ) + MinInterval;
        if ( CurrentDirection == Direction.Decrease )
            newInterval = -newInterval;

        if ( CurrentValue + newInterval < MinValue )
            CurrentValue = MinValue;
        else if ( CurrentValue + newInterval > MaxValue )
            CurrentValue = MaxValue;
        else
            CurrentValue += newInterval;

        return CurrentValue;
    }
}

用法示例

//The next code simulates data changes every 500 ms
Timer = new Timer
{
    Interval = 1000
};
Timer.Tick += TimerOnTick;

var vg = new ValueGenerator( new Random( ) )
{
    MinValue = 0,
    MaxValue = 5,
    MinInterval = 0.1,
    MaxInterval = 0.7,
    CurrentValue = 3.0,
    CurrentDirection = Direction.Decrease,
    PossibilityToChangeDirection = 10,
};

Timer.Start();

ChartValues.Add(new MeasureModel
{
    DateTime = now,
    Value = vg.NextValue(),
});