I need help to generate a unique random sine translation for every branch of my grasshopper DataTree. I have scripted this C# code but each branch is a repetition. Can anyone help?
// Initialize the output DataTree
DataTree/span>double> R = new DataTree/span>double>();
for (int branchNo = 0; branchNo /span> Values.BranchCount; branchNo++)
{
// Create a unique seed for each branch using the branch number
Random random = new Random(branchNo * DateTime.Now.Microsecond);
// Generate random values for each item
double a = (random.NextDouble() * 2 - 1); // Random double between -1 and 1
double d = random.NextDouble(); // Random double between 0 and 1
double b = random.NextDouble(); // Random double between 0 and 1
double c = (random.NextDouble() * 2 - 1); // Random double between -1 and 1
// Loop through each item in the branch
foreach (var item in Values.Branch(branchNo))
{
// Calculate V using the formula
double V = a * Math.Sin(b * item + c) + d;
// Create path for DataTree insertion
GH_Path path = new GH_Path(branchNo);
// Check if value already exists in R
bool exists = R.AllData().Contains(V);
// If value does not exist, add it to the DataTree
if (!exists)
{
R.Add(item, path);
}
}
}
// Output the result
Results = R;
I can barely read C# but I guess you are creating V, then checking if V is already present, and at the end instead of inserting that V you insert “item” ?
// Initialize the output DataTree
DataTree<double> R = new DataTree<double>();
// Used to provide more randomness
Random timeRandomizer = new Random();
int timeSeedModifier = timeRandomizer.Next(1000, 10000);
for (int branchNo = 0; branchNo < Values.BranchCount; branchNo++)
{
// Create a unique seed for each branch using the branch number and a time component
Random random = new Random(branchNo * timeSeedModifier + (int)DateTime.Now.Ticks);
// Generate random values for each item
double a = (random.NextDouble() * 2 - 1); // Random double between -1 and 1
double d = random.NextDouble(); // Random double between 0 and 1
double b = random.NextDouble(); // Random double between 0 and 1
double c = (random.NextDouble() * 2 - 1); // Random double between -1 and 1
// Create path for DataTree insertion
GH_Path path = new GH_Path(branchNo);
// Loop through each item in the branch
foreach (var item in Values.Branch(branchNo))
{
// Calculate V using the formula
double V = a * Math.Sin(b * item + c) + d;
// Add value to the DataTree
R.Add(V, path); // Assuming you want to store the transformed value
}
}
// Output the result
Results = R;
:
Random Seed: Combined a stable timeSeedModifier with branch index multipliers to ensure unique seeds across branches.
Value to DataTree: Directly add the transformed value VV to the DataTree.
Avoid AllData Check: Removed the check for existing values within the entire DataTree to improve performance and because each transformation is inherently probabilistically unique.!