Hello C# World!
I try to figure out how the Gh Random component works,
I wonder if there is not a simple way to do it?
Because I had to write many if conditions…
I will appreciate any comment!
thanks!
private void RunScript(Interval r, int n, int s, ref object A, ref object R)
{
// Random.Next Method
// //Returns a non-negative random integer.
// // https://docs.microsoft.com/en-us/dotnet/api/system.random.next?view=netcore-3.1
// Random.NextDouble
// //Returns a random floating-point number 0.0 to 1.0
// //https://docs.microsoft.com/en-us/dotnet/api/system.random.nextdouble?view=netcore-3.1
Random rnd = new Random(s);
Interval randomDomain = new Interval(r);
//r.Min;
if (n < 1)
{
Component.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, " n = 0! minimum n >= 1 ");
return;
}
if (n == 1)
{
double rand = rnd.NextDouble(); //Random.NextDouble()
A = rand;
//randomDomain.Min;
double scaleRand = rand * randomDomain.Max;
R = scaleRand;
}
if ((n > 1) && (r.Min == 0 ))
{
List < double > collectRandomNext = new List<double>();
List<double> collectRandom = new List<double>();
for (int i = 0; i < n; i++)
{
double rand = rnd.NextDouble();
collectRandomNext.Add(rand);
double scaleRand = rand * randomDomain.Max;
collectRandom.Add(scaleRand);
}
A = collectRandomNext;
R = collectRandom;
}
if ((n > 1) && (r.Min >= 1 ) && (r.Min < r.Max) )
{
List < double > collectRandomNext = new List<double>();
List<double> collectRandom = new List<double>();
for (int i = 0; i < n; i++)
{
double rand = rnd.NextDouble();
collectRandomNext.Add(rand);
double scaleRand = ((r.Max - r.Min) * rand) + r.Min;
//double scaleRand = rand * randomDomain.Max;
collectRandom.Add(scaleRand);
}
A = collectRandomNext;
R = collectRandom;
}
if (r.Min == r.Max)
{
List<double> noRandom = new List<double>();
for (int i = 0; i < n; i++)
{
double nRandom = r.Min;
noRandom.Add(nRandom);
}
A = r.Min;
R = noRandom;
}
}
RandomNextDouble.gh (19.0 KB)