C# Random.NextDouble()

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)

private void RunScript(Interval domain, int count, int seed){
  var rnd = new Random(seed);
  var list = new List<double>();
  for(var i=0; i<count; i++){
    list.Add(domain.ParameterAt(rnd.NextDouble()));
  }
  A = list;
}
1 Like

Dear Dani_Abalde,
Thank you very much for your answer!
By the way I notice something : when the (domain.ParameterAt(0) > domain.ParameterAt(1)) the output are not Equal. So I did this condition :
Random.gh (14.6 KB)

Please let me know any observation! there is not a Method to flip an Interval?
Best!
Andres

private void RunScript(Interval domain, int count, int seed, ref object A, ref object B)
{

Interval inverDomain = new Interval(domain.Min, domain.Max);

var rnd = new Random(seed);
var list = new List<double>();

if (domain.ParameterAt(0) <= domain.ParameterAt(1))
{
  for(var i = 0; i < count; i++)
  {
    list.Add(domain.ParameterAt(rnd.NextDouble()));
  }
}

else if (domain.ParameterAt(0) > domain.ParameterAt(1))
{
  for(var i = 0; i < count; i++)
  {
    list.Add(inverDomain.ParameterAt(rnd.NextDouble()));
  }

}

A = list;
B = inverDomain;

}