Hey Guys,
I have a Tree of Points that I want to populate with A,B,C & D.
for the given length of branch I calculate percent of points that each of these letters will be placed on.
where for each A, it places 1 A , for each B, it places 3 B, for each C it places 2 C & for each D it places 3 D for the length of each branch.
the issue I have is that instead of generating random order per branch, it is simply assigning the same randomness to all the branches. Here is the Code:
private void RunScript(DataTree<Point3d> x, List<string> inputText, ref object A, ref object B)
{
DataTree<Point3d> randomPts = new DataTree<Point3d>();
DataTree<string> allitemID = new DataTree<string>();
int j = 0;
List<string> randomizedText = Randomize(inputText);
foreach(var branch in x.Branches)
{
List<string> itemID = new List<string>();
int totalCount = branch.Count; //Incase of List
int actualdCount = RoundPercentageToInt(totalCount, 0.20, 3); //represents d input
int actualcCount = RoundPercentageToInt(totalCount, 0.30, 2); //represents c input
int actualbCount = RoundPercentageToInt(totalCount, 0.20, 3); //represents b input
int actualaCount = totalCount - (actualbCount + actualcCount + actualdCount); //represents a input
foreach(var text in randomizedText)
{
if(text == "A")
{
for(int i = 0; i < actualaCount; i++)
{
itemID.Add(text);
}
}
else if(text == "B")
{
for(int i = 0; i < actualbCount; i++)
{
itemID.Add(text);
}
}
else if(text == "C")
{
for(int i = 0; i < actualcCount; i++)
{
itemID.Add(text);
}
}
else
{
for(int i = 0; i < actualdCount; i++)
{
itemID.Add(text);
}
}
}
allitemID.AddRange(itemID, new GH_Path(j));
randomPts.AddRange(branch, new GH_Path(j));
j++;
}
A = randomPts;
B = allitemID;
}
// <Custom additional code>
public int RoundPercentageToInt(int dividend, double percent, int divisor)
{
int roundtoInt;
int dCount = (int) (dividend * percent);
if (dCount % divisor == 0)
{
roundtoInt = dCount;
}
else
{
if(dCount % divisor > 1)
{
roundtoInt = dCount + 1;
}
else
{
roundtoInt = dCount - dCount % divisor;
}
}
return roundtoInt;
}
//Randomise a list
public List<string> Randomize(List<string> list)
{
List<string> randomizedList = new List<string>();
while (list.Count > 0)
{
Random iRandom = new Random();
int index = iRandom.Next(0, list.Count); //pick a random item from the master list
randomizedList.Add(list[index]); //place it at the end of the randomized list
list.RemoveAt(index);
}
return randomizedList;
}
You’re using the same list of randomized text for each branch — I think you meant to randomize the list differently for each branch. I’m not sure this is exactly what you were after but it does generate different values per branch. It’s important to know that the Random class will always generate the same sequence unless you give it a different seed value.