# C# Random remove when coming closer to a target number

I have a list of numbers. With C# I want to remove the numbers gradually randomly when numbers come closer to a target number.

I now how to do something with a target number in Python

``````def randomIntegerVa(startVa,endVa,targetVa=None):
if targetVa!=None:
randomIntegerVa=round(r.triangular(startVa,endVa,targetVa))
else:
randomIntegerVa=r.randint(startVa,endVa)
return randomIntegerVa
``````

I was thinking about something with divide or the remainder to increase the change a number is removed. But that does not sound that smart because it has nothing to do with ‘randomness.’

Maybe I should first remap-ly randomly remove.

A. Remove randomly x-amount of items.
B. Increase change of removal when closer to target number.

1# Question:
Is MathNet a thing in C#?

It does not seem like it.
I entered ‘using MathNet;’

Hi @YellowLion,

– Dale

Am I missing something?

I am aware of that function. To what you are pointing to? A triangular function or something like that? Is it something in the examples on that page?

When a values comes closer to a target number a sub list of 0’s and 1’s should be increase or there should be compared to a minimum and maximum closest to the target number a specific amount of 0’s and 1’s by which randomly an item is choosing selecting or deselecting a value of my list.

So.

values 0, 2, 3, 4, 9, 22, 28

target 3

max to target is 28

value 3 makes [0, 1, 1, 1, 1] => 0 is 20% of the time
value 28 makes [0, 0, 0, 1, 0] => 0 is 80% of the time

Or something like that.

But you were pointing to a random.triangular or something?
Does it exist?

Sorry - I thought you were looking at another library to provided random number generation. I was only point out that .NET has one.

If looks like Math.NET is available via NuGet.

– Dale

1 Like

you find the current phyton implementation of the random triangular function here
Line 507 onwards…

`````` def triangular(self, low=0.0, high=1.0, mode=None):
"""Triangular distribution.
Continuous distribution bounded by given lower and upper limits,
and having a given mode value in-between.
http://en.wikipedia.org/wiki/Triangular_distribution
"""
u = self.random()
try:
c = 0.5 if mode is None else (mode - low) / (high - low)
except ZeroDivisionError:
return low
if u > c:
u = 1.0 - u
c = 1.0 - c
low, high = high, low
return low + (high - low) * _sqrt(u * c)
``````

… translate those 6 lines of code into (12? ) c#
kind regards -tom

1 Like

Dear Tom

I am aware of some typos in my script which I will later solve.
But about Null or None, do you might know what type it is It is not double

triangular001.gh (3.6 KB)

Hello
From what I understand if mode is None it means there is no value so by default mode is set to middle => 0.5. But Random in Python is surely between 0 and 1. So why I use Random.NextDouble()
Here for the script I count how many value are on the interval in order to test the distribution. It seems OK.

I use that from Wikipedia

`````` double triangularCentered(Random random, double low, double high, double middle)
{
double u = random.NextDouble();

if (low > high)
{
double lowBuffer = low;
low = high;
high = lowBuffer;
}

if (middle < low) middle = low;
if (middle > high) middle = high;

double result = low;
if (Math.Abs(high - low) > 0.0)
{
if (u < (middle - low) / (high - low))
{
result = low + Math.Sqrt(u * (high - low) * (middle - low));
}
else
{
result = high - Math.Sqrt((1 - u) * (high - low) * (high - middle));
}
}
return result;
}
``````

triangular001.gh (10.0 KB)

1 Like

in the phyton code, the low, high, mode are optional parameters, set to 0,1,None:
`(self, low=0.0, high=1.0, mode=None)

similar is possible in c#, dependent on wether it is a object type (check for “null”) or a value type. (check for the topic “nullable”, or for Double.NaN… )
`(double low = 0.0, double high = 1.0, double mode = Double.NaN)`
in the context of Rhino, you should use