Skip to content

Commit ddbab17

Browse files
authored
Modify some of the examples to make them deterministic (#2091)
1 parent 3a54eb5 commit ddbab17

7 files changed

Lines changed: 35 additions & 39 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ All notable changes to this project will be documented in this file.
2828
- Update SkiaSharp to Version 2.88.6
2929
- AxisRendererBase is now generic
3030
- DateTimeAxis.ToDateTime(double value) is now obsolete, replacements are provided (related to #2061)
31+
- Modify some of the examples to make them deterministic
3132

3233
### Removed
3334
- Support for .NET Framework 4.0 and 4.5 (#1839)

Source/Examples/ExampleLibrary/Issues/Issues.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -741,22 +741,24 @@ public static PlotModel DataPointsRemainVisibleOutsideBoundsOnPanning()
741741

742742
plotModel1.Axes.Add(verticalAxis);
743743

744+
var time = new DateTime(2024, 07, 20, 13, 27, 5);
745+
744746
var line = new LineSeries { Title = "Measurement", XAxisKey = masterAxis.Key, YAxisKey = verticalAxis.Key };
745-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), 10));
746-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(1)), 10));
747-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(2)), 45));
748-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(3)), 17));
747+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time), 10));
748+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time.AddSeconds(1)), 10));
749+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time.AddSeconds(2)), 45));
750+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time.AddSeconds(3)), 17));
749751

750752
line.Points.Add(DataPoint.Undefined);
751753

752754
// this point should be visible
753-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(4)), 10));
755+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time.AddSeconds(4)), 10));
754756
//// line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(4)), 10));
755757

756758
line.Points.Add(DataPoint.Undefined);
757759

758-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(5)), 45));
759-
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now.AddSeconds(6)), 17));
760+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time.AddSeconds(5)), 45));
761+
line.Points.Add(new DataPoint(DateTimeAxis.ToDouble(time.AddSeconds(6)), 17));
760762

761763
plotModel1.Series.Add(line);
762764

@@ -1184,7 +1186,7 @@ public static PlotModel ImageAnnotationWithWidthHeightCrashes()
11841186
var myModel = new PlotModel { Title = "Example 1" };
11851187
myModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
11861188

1187-
var rng = new Random();
1189+
var rng = new Random(0);
11881190
var buf = new byte[100, 100];
11891191
for (int i = 0; i < 100; i++)
11901192
{
@@ -2230,7 +2232,7 @@ public static PlotModel HitTrackerIndexOutOfRangeExceptionWithHeatMapSeries()
22302232
Palette = myPalette,
22312233
});
22322234

2233-
var rand = new Random();
2235+
var rand = new Random(0);
22342236
var data = new double[yAxisLabels.Count, xAxisLabels.Count];
22352237
for (int x = 0; x < xAxisLabels.Count; ++x)
22362238
{

Source/Examples/ExampleLibrary/Series/FinancialSeries/CandleStickAndVolumeSeriesExamples.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private static Example CreateCandleStickAndVolumeSeriesExample(
127127
};
128128

129129
// create bars
130-
foreach (var bar in OhlcvItemGenerator.MRProcess(n))
130+
foreach (var bar in OhlcvItemGenerator.MRProcess(n, new Random(0)))
131131
{
132132
series.Append(bar);
133133
}
@@ -232,4 +232,4 @@ private static void AdjustYExtent(CandleStickAndVolumeSeries series, DateTimeAxi
232232
yaxis.Zoom(ymin - margin, ymax + margin);
233233
}
234234
}
235-
}
235+
}

Source/Examples/ExampleLibrary/Series/FinancialSeries/CandleStickSeriesExamples.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static Example LargeDataSetWide()
2626
var linearAxis1 = new LinearAxis { Position = AxisPosition.Left };
2727
pm.Axes.Add(linearAxis1);
2828
var n = 1000000;
29-
var items = HighLowItemGenerator.MRProcess(n).ToArray();
29+
var items = HighLowItemGenerator.MRProcess(n, new Random(0)).ToArray();
3030
var series = new CandleStickSeries
3131
{
3232
Color = OxyColors.Black,
@@ -68,7 +68,7 @@ public static Example LargeDataSetNarrow()
6868
var linearAxis1 = new LinearAxis { Position = AxisPosition.Left };
6969
pm.Axes.Add(linearAxis1);
7070
var n = 1000000;
71-
var items = HighLowItemGenerator.MRProcess(n).ToArray();
71+
var items = HighLowItemGenerator.MRProcess(n, new Random(1)).ToArray();
7272
var series = new CandleStickSeries
7373
{
7474
Color = OxyColors.Black,
@@ -106,7 +106,7 @@ public static Example SmallDataSet()
106106
var linearAxis1 = new LinearAxis { Position = AxisPosition.Left };
107107
pm.Axes.Add(linearAxis1);
108108
var n = 100;
109-
var items = HighLowItemGenerator.MRProcess(n).ToArray();
109+
var items = HighLowItemGenerator.MRProcess(n, new Random(2)).ToArray();
110110
var series = new CandleStickSeries
111111
{
112112
Color = OxyColors.Black,

Source/Examples/ExampleLibrary/Series/FinancialSeries/HighLowItemGenerator.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ namespace ExampleLibrary
2020
/// </summary>
2121
public static class HighLowItemGenerator
2222
{
23-
/// <summary>
24-
/// The random number generator.
25-
/// </summary>
26-
private static readonly Random Rand = new Random();
27-
2823
/// <summary>
2924
/// Creates bars governed by a MR process
3025
/// </summary>
@@ -36,19 +31,20 @@ public static class HighLowItemGenerator
3631
/// <param name="kappa">Kappa.</param>
3732
public static IEnumerable<HighLowItem> MRProcess(
3833
int n,
34+
Random rand,
3935
double x0 = 100.0,
4036
double csigma = 0.50,
4137
double esigma = 0.70,
4238
double kappa = 0.01)
4339
{
4440
double x = x0;
4541

46-
var baseT = DateTime.UtcNow;
42+
var baseT = new DateTime(2024, 07, 20, 14, 26, 06);
4743
for (int ti = 0; ti < n; ti++)
4844
{
49-
var dx_c = -kappa * (x - x0) + RandomNormal(0, csigma);
50-
var dx_1 = -kappa * (x - x0) + RandomNormal(0, esigma);
51-
var dx_2 = -kappa * (x - x0) + RandomNormal(0, esigma);
45+
var dx_c = -kappa * (x - x0) + RandomNormal(0, csigma, rand);
46+
var dx_1 = -kappa * (x - x0) + RandomNormal(0, esigma, rand);
47+
var dx_2 = -kappa * (x - x0) + RandomNormal(0, esigma, rand);
5248

5349
var open = x;
5450
var close = x = x + dx_c;
@@ -92,9 +88,9 @@ private static double Max(double a, double b, double c, double d)
9288
/// </summary>
9389
/// <param name="mu">Mu.</param>
9490
/// <param name="sigma">Sigma.</param>
95-
private static double RandomNormal(double mu, double sigma)
91+
private static double RandomNormal(double mu, double sigma, Random rand)
9692
{
97-
return InverseCumNormal(Rand.NextDouble(), mu, sigma);
93+
return InverseCumNormal(rand.NextDouble(), mu, sigma);
9894
}
9995

10096
/// <summary>
@@ -194,4 +190,4 @@ private static double CumN0(double x)
194190
}
195191
}
196192
}
197-
}
193+
}

Source/Examples/ExampleLibrary/Series/FinancialSeries/OhlcvItemGenerator.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ namespace ExampleLibrary
2020
/// </summary>
2121
public static class OhlcvItemGenerator
2222
{
23-
/// <summary>
24-
/// The random number generator.
25-
/// </summary>
26-
private static readonly Random Rand = new Random();
27-
2823
/// <summary>
2924
/// Creates bars governed by a MR process.
3025
/// </summary>
@@ -39,19 +34,20 @@ public static class OhlcvItemGenerator
3934
/// </returns>
4035
public static IEnumerable<OhlcvItem> MRProcess(
4136
int n,
37+
Random rand,
4238
double x0 = 100.0,
4339
double v0 = 500,
4440
double csigma = 0.50,
4541
double esigma = 0.75,
4642
double kappa = 0.01)
4743
{
4844
double x = x0;
49-
var baseT = DateTime.UtcNow;
45+
var baseT = new DateTime(2024, 07, 20, 14, 26, 06);
5046
for (int ti = 0; ti < n; ti++)
5147
{
52-
var dx_c = -kappa * (x - x0) + RandomNormal(0, csigma);
53-
var dx_1 = -kappa * (x - x0) + RandomNormal(0, esigma);
54-
var dx_2 = -kappa * (x - x0) + RandomNormal(0, esigma);
48+
var dx_c = -kappa * (x - x0) + RandomNormal(0, csigma, rand);
49+
var dx_1 = -kappa * (x - x0) + RandomNormal(0, esigma, rand);
50+
var dx_2 = -kappa * (x - x0) + RandomNormal(0, esigma, rand);
5551

5652
var open = x;
5753
var close = x = x + dx_c;
@@ -106,9 +102,9 @@ private static double Max(double a, double b, double c, double d)
106102
/// <param name="mu">Mu.</param>
107103
/// <param name="sigma">Sigma.</param>
108104
/// <returns></returns>
109-
private static double RandomNormal(double mu, double sigma)
105+
private static double RandomNormal(double mu, double sigma, Random rand)
110106
{
111-
return InverseCumNormal(Rand.NextDouble(), mu, sigma);
107+
return InverseCumNormal(rand.NextDouble(), mu, sigma);
112108
}
113109

114110
/// <summary>
@@ -208,4 +204,4 @@ private static double CumN0(double x)
208204
}
209205
}
210206
}
211-
}
207+
}

Source/Examples/ExampleLibrary/Series/FinancialSeries/VolumeSeriesExamples.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ExampleLibrary
99
using OxyPlot;
1010
using OxyPlot.Axes;
1111
using OxyPlot.Series;
12+
using System;
1213

1314
[Examples("VolumeSeries")]
1415
[Tags("Series")]
@@ -78,7 +79,7 @@ private static Example CreateVolumeSeries(
7879
};
7980

8081
// create bars
81-
foreach (var bar in OhlcvItemGenerator.MRProcess(n))
82+
foreach (var bar in OhlcvItemGenerator.MRProcess(n, new Random(1)))
8283
{
8384
series.Append(bar);
8485
}

0 commit comments

Comments
 (0)