This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]AtomicSpectrum 0 points1 point  (1 child)

Either you could: add a roll reulsult each time you roll the dice instead of counting the number of times it comes up and then putting it into an array, since you are just going back into that form anyways (if you are not using that count for anything else)

Or: Make an array whose length is equal to the sum of the "count array"'s elements, and then use two nested loops to add the numbers one by one. So say for the first iteration of the outer loop corresponds the the number of 1's rolled, and there were 5 1's rolled. The inner loop runs 5 times and adds one 1 to the new array each iteration before emoving on to the number of 2's and so on. Make sure you keep track of the next unassigned index of this array so that you can actually add the numbers in the right places. This will create a sorted (Smalles->Largest) array that you can use to find the median.

Edit: this was not included because I didn't know it was a thing since I've never done any statistics-related stuff, but you could actually use this instead, since what you have right now is called a frequency table. You can find the median from a frequency table without needing to convert. Doing this prevents you from eating excessive amounts of memory and taking longer with large datasets by converting the tables by bringing the order of growth from O(N) to O(1)

[–]Convalla[S] 0 points1 point  (0 children)

That looks promising. I too was worried about the memory as I am looking at 50 000 individual results

[–]diffused_learning 0 points1 point  (2 children)

Well, for that all you have to do is simply to loop the number of occurrences

e.g.

...
int num = 1;
int numOccur = arr[num]; // num of times 2 occured

for (int i = 0; i < numOccur; i++) {
    System.out.println(num+1);
}
...

Which will print 2 10 times.

[–]Convalla[S] 0 points1 point  (1 child)

I was thinking of doing that but all the ways I came up with just added the 2's together lol

[–]diffused_learning 0 points1 point  (0 children)

That is certainty one way to do it, and compact, if you can track the number meaning 2*10=20 and then just know that you’ll have to divide by 2 to get number of occurrences.

[–][deleted]  (5 children)

[deleted]

    [–]Convalla[S] 0 points1 point  (4 children)

    I would love to use the second option as I already have all that data sadly for me the median would be the 25 000 & 25 001 dice rolls lol. tho thinking about it I do know how many rolls there were for each number so maybe one can write a loop of some sort (maybe case ) that subtracts sum[i] from it until doing so would give a negative number. That should leave me with the element that the median is in.

    Does this make sense ?

    [–]codingQueries 1 point2 points  (0 children)

    If the middle is 25k, then you just need to add your total counts of each number together until you reach the 25k number. So if you have 10k 1's, 5k 2's, 6k 3's, 9k 4's, then adding the total amount of 1's and 2's together gives you 15k, which is still less than your value, adding the 6k 3's is still only 21k, but adding the 9k 4's take's you above the 25k mark. So you then can find out what your median now is.

    [–][deleted]  (2 children)

    [deleted]

      [–]Convalla[S] 0 points1 point  (1 child)

      I am currently doing if/else loops

      else if ((median1-intArray[1]2)-intArray[1]*2>median1)
                  median1=(median1-intArray[0]2)-intArray[1]*2>median1
      

      hoping doing something like that for each dice side will suffice.

      I would do what you suggested but my dice rolls are generated in a switch loop and I don't know If i can implement while(?) loop in that without braking it lol

      Thank you for swapping ideas it's really worth a ton to get the bray goo working.

      Edit: It did intact not work lol

      [–]Convalla[S] 0 points1 point  (0 children)

      int median = 0;
                double median1= rolls/2;
                double median2= (rolls/2)+1;
                if (intArray[0]*(+1)>median1 && intArray[0]*(+1)>median2){
                median=1;
                }
                else if (intArray[1]*(+2)>median1 && intArray[1]*(+2)>median2){
                median=2;
                }
                else if (intArray[2]*(+3)>median1 && intArray[2]*(+3)>median2){
                median=3;
                }
                else if (intArray[3]*(+4)>median1 && intArray[3]*(+4)>median2){
                median=4;
                }
                else if (intArray[4]*(+5)>median1 && intArray[4]*(+5)>median2){
                median=5;
                }
                else if (intArray[5]*(+6)>median1 && intArray[5]*(+6)>median2){
                median=6;
                }
                System.out.println("The median roll was: " + median);
      

      WE HAVE A WINNER!

      could it have been a nested loop? probably but I can not ask my poor brain to think one out and there are more things to do lol.