How in array random delete and add units? For example if have array 1011100101 of length 10 with 6 units and 4 zeros, how to get array of length 10 with 3 units and 7 zeros? Or if have array 100100000 of length 10 with 2 units and 8 zeros, how to get array of length 10 with 5 units and 5 zeros? I tried something like this:
int units = array.getUnits();
if (units > P)
{
while (units != P)
{
int p = rnd.Next(units), pos = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 1)
pos++;
if (pos == p)
{
array[p]=0;
break;
}
}
units--;
}
}
else if (units < P)
{
while (units != P)
{
int p = rnd.Next(array.Length-units),
pos = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 0)
pos++;
if (pos == p)
{
array[p]=1;
break;
}
}
units++;
}
}
It only add one unit (not 2 or more) or delete one unit.