Why, when I compare these two random integer arrays with known sum X, do i get the same values for sums of their respective differences?
NOTE: This if for a personal project and not for a class, the code I entered was compiled from sources on the internet and I have little programming knowledge.
EXERCISE: I am trying to generate two arrays , size X and Y, that sum to known sums M and N. I am then trying to compare the arrays values and if rnd.x is greater than rnd.y it adds to a sum of X difference from Y or if the values of Y are greater than X it adds to a sum of Y difference from X.
PROBLEM: I am having a problem that when I generate the numbers and compare values I am getting equal values for sum X and Y, when this seems illogical if I am comparing random numbers. When the valueD is changed from say 80 to 100 and valueA remains(80) then the result of the sums, though random, always has a difference of 20. What is happening? Why aren't the sums X and Y "ASum, DSum" different? I feel like the answer is simple math logic that I don't understand or maybe it has to do with my swap functions?
using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.Diagnostics; using UnityEngine;
public class NumberGenerator3 : MonoBehaviour {
// Use this for initialization
void Start()
{
int ASum = 0;
int DSum = 0;
int count = 19;
int countA = 19;
int AValue = 80;
int DValue = 80;
int[] values1 = getNumbers(count, AValue);
int[] values2 = getNumbersA(countA, DValue);
int[][] JaggedValues = new int[2][];
{
for (int i = 0; i < count; i++)
{
JaggedValues[0] = values1;
UnityEngine.Debug.Log(values1[i]);
JaggedValues[1] = values2;
UnityEngine.Debug.Log(values2[i]);
if (values1[i] > values2[i])
{
UnityEngine.Debug.Log(values1[i] - values2[i] + "A");
ASum += values1[i] - values2[i];
}
else
{
UnityEngine.Debug.Log(values2[i] - values1[i] + "D");
DSum += values2[i] - values1[i];
}
UnityEngine.Debug.Log(" ");
}
UnityEngine.Debug.Log(ASum + "AVT");
UnityEngine.Debug.Log(DSum + "DVT");
}
}
private static System.Random rnd;
static NumberGenerator3()
{
rnd = new System.Random();
}
static int[] getNumbers(int count, int total)
{
const int LOWERBOUND = 1;
const int UPPERBOUND = 8;
int[] result = new int[count];
int currentsum = 0;
int low, high, calc;
if ((UPPERBOUND * count) < total ||
(LOWERBOUND * count) > total ||
UPPERBOUND < LOWERBOUND)
throw new Exception("Not possible.");
for (int index = 0; index < count; index++)
{
calc = (total - currentsum) - (UPPERBOUND * (count - 1 - index));
low = calc < LOWERBOUND ? LOWERBOUND : calc;
calc = (total - currentsum) - (LOWERBOUND * (count - 1 - index));
high = calc > UPPERBOUND ? UPPERBOUND : calc;
result[index] = rnd.Next(low, high + 1);
currentsum += result[index];
}
int shuffleCount = rnd.Next(count * 5, count * 10);
while (shuffleCount-- > 0)
Swap(ref result[rnd.Next(0, count)], ref result[rnd.Next(0, count)]);
return result;
}
public static void Swap(ref int item1, ref int item2)
{
int temp = item1;
item1 = item2;
item2 = temp;
}
static int[] getNumbersA(int count, int total)
{
const int LOWERBOUND = 1;
const int UPPERBOUND = 8;
int[] result = new int[count];
int currentsum = 0;
int low, high, calc;
if ((UPPERBOUND * count) < total ||
(LOWERBOUND * count) > total ||
UPPERBOUND < LOWERBOUND)
throw new Exception("Not possible.");
for (int index = 0; index < count; index++)
{
calc = (total - currentsum) - (UPPERBOUND * (count - 1 - index));
low = calc < LOWERBOUND ? LOWERBOUND : calc;
calc = (total - currentsum) - (LOWERBOUND * (count - 1 - index));
high = calc > UPPERBOUND ? UPPERBOUND : calc;
result[index] = rnd.Next(low, high + 1);
currentsum += result[index];
}
int shuffleCount = rnd.Next(count * 5, count * 10);
while (shuffleCount-- > 0)
SwapA(ref result[rnd.Next(0, count)], ref result[rnd.Next(0, count)]);
return result;
}
public static void SwapA(ref int item3, ref int item4)
{
int tempA = item3;
item3 = item4;
item4 = tempA;
}
}
Your answer
Follow this Question
Related Questions
I am trying to display two scores and use them to trigger events. 1 Answer
List or Arrays, and how to randomize them. 1 Answer
I'm trying to shuffle an array's order 3 Answers
null array? 0 Answers