c# - Is There A Better Way to Add Entropy to Array of Ints? -


i needed take array of (c#) integers , randomly reassign values make values "feel" more randomized, without changing length or sum of array. array can quite large, i'm wondering if has better way this. basically, array contains values sum divided length, 1 element having remainder. using is:

    static int[] addentropy(int[] ia)     {          int elements = ia.length;         int sum = ia.sum();          (int runs = 0; runs < (elements * 2); runs++)         {              random rnd = new random(int.parse(guid.newguid().tostring().substring(0, 8), system.globalization.numberstyles.hexnumber));             int rndi = rnd.next(0, (sum / elements));             int rnde1 = rnd.next(0, elements);             int rnde2 = rnd.next(0, elements);             if (rndi < 1 ) rndi = 1;              if (ia[rnde1] > (rndi + 2))             {                 ia[rnde1] = ia[rnde1] - rndi;                 ia[rnde2] = ia[rnde2] + rndi;             }          }          return ia;     } 

any thoughts on making perform better appreciated. seems perform "well", if array larger sample of 5 elements (up 1000 elements), , there several arrays may modified in quick succession, speedier option great.

if understood correctly task requires sum of elements preserved. there no need in initial array elements values, 2 thing matter sum of elements , number of elements.

public static int[] getarray(int sum, int n) {     if(sum < n)         throw new argumentexception("sum lower n");     random rnd = new random();      // reserve 1 each of elements     sum -= n;      // generate random weights every element in sum     int[] w = new int[n];     int sw = 0;                  (int = 0; < n; i++)     {         w[i] = rnd.next(0, 100);         sw += w[i];     }      // generate element values based on weights     int[] result = new int[n];     int tsum = 0;     int psum = 0;     (int = 0; < n; i++)     {         tsum += w[i] * sum;         result[i] = tsum / sw - psum;         psum += result[i];     }      // restore reserved ones     (int = 0; < n; i++)         result[i]++;      return result; } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -