Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ActualCheddar · Sep 18, 2018 at 11:14 PM · randomarrays

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Spawning random pickups (from Array) into randomly chosen spawn positions (also from an array) and preventing pickups from spawning on top of each other. 0 Answers

I'm trying to shuffle an array's order 3 Answers

null array? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges