Grasshopper

algorithmic modeling for Rhino

Hey Everyone! 

I am in front of another problem. I have 50 arrays in an array. First every array contains the same order of points. Next every array should be shuffled differently. That means every array should have a random order of points. I found RNGCryptoServiceProvider online and tried to shuffle my arrays with it, but it somehow does not shuffle correctly. 

Does anyone has any idea whats wrong? Where is the error causing this. You can see in the picture below that after shuffeling the arrays, the arrays sometimes do not contain every points, instead it contains zero. Why is that happening? 

here is the code: 

Point3d[][] Population = new Point3d[51][];
for(int i = 0; i < Population.Length; i++)
{
    Population[i] = new Point3d[pts.Count];
    for(int j = 0; j < pts.Count; j++)
    {
      Population[i][j] = pts[j];

      System.Security.Cryptography.RNGCryptoServiceProvider provider = new System.Security.Cryptography.RNGCryptoServiceProvider();


      int n = Population[i].Length;
      while (n > 0)
       {
         byte[] box = new byte[1];
         do provider.GetBytes(box);
         while (!(box[0] < n * (Byte.MaxValue / n)));
         int k = (box[0] % n);
         n--;
         Point3d value = Population[i][k];
         Population[i][k] = Population[i][n];
         Population[i][n] = value;
       }
    }

}

 

Views: 1073

Replies to This Discussion

If you don't like Linq, you can reorder your arrays by sorting them using a random key array.

double[] keys = new double[points.Length];
for (int i = 0; i < points.Length; i++)
keys[i] = random.NextDouble();
Array.Sort(keys, points);

However this does not guarantee that all results will be different. It is possible you generate two random arrays that sort the same way. You'll need additional logic to detect these cases.

Actually I would like to use this because I read that RNGCryptoServiceProvider provides a better 'shuffle'.

Define 'better'. There are more advanced random engines than the vanilla System.Random, but whether or not you need any of those depends on the problem you're solving. If you're just shaking things up a bit to provide a random starting point for some algorithm I doubt it matters much. If on the other hand you're generating encryption keys that are supposed to stop Russian hackers in their tracks you may want to opt for a more cutting edge solution.

List<T> is indeed a lot more functional than T[], but with that functionality comes a hit in performance and memory overhead. Its also not possible to have multi-dimensional lists, but then I think multi-dimensional arrays are evil anyway and should be avoided whenever possible.

If you know ahead of time how many items you want to store and that number never changes, you might as well use an array. However if you need to be able to add, remove or insert values then a list is usually the best option.

One important thing to realise when using random generators is that they are associated with a specific seed value. Every time you use the same seed, you get the same random numbers. In my experience you always want to use the same seed, otherwise the algorithm becomes unpredictable. Successive runs, even with the exact same inputs, may yield different results. This is why all Grasshopper components that in some way use a random process always have a seed input.

I can't look at your file today because I'm having trouble compiling Rhino at the moment, and I'm out of office tomorrow. 

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service