-1

I'm creating a simple .php script that

  1. Generates an array with random numbers (let's say 1000 integers)
  2. Outputs an image, where it displays all these numbers' arrangement on a scale from 0 to the largest number drawn

The output is an image (with given width), this is what I got:

image output example

As you can see, at some points the bars are higher. It's because the image usually isn't wide enough to present all the numbers, so if there are more numbers laying on one pixel, then the bar is going higher.

The problem: Sometimes this script works well, but occasionally I get numerous Notice errors:

Notice: Undefined offset: 398 in /.../index.php on line 61

Notice: Undefined offset: 125 in /.../index.php on line 61

Notice: Undefined offset: 192 in /.../index.php on line 61 ...

The script is below, some explanations:

  • Line 61 marked red (below)
  • function toScale() returns intval( number / maxNumber * scaleWidth )
  • inb4 unnecessary double declaration of $counts[num]=0, that's what I've done trying to solve the problem, it doesn't seem to change anything
  • $counts isn't used anywhere else in between these two parts
  • the number of Notice errors is never equal to the total numbers count, it's either a few or none of these.

code screenshot

Update: As Barmar mentioned, the second loop from my code can be replaced with

$max = max($data); $counts = array_count_values(array_map('toScale', $data));

It looks like just code simplification, but somehow this solves the problem. Why?

Community
  • 1
  • 1
Lis
  • 555
  • 4
  • 26
  • 4
    Post your code as plain text, not an image. See [formatting](https://stackoverflow.com/help/formatting) for code formatting help. – Barmar Nov 16 '18 at 00:43
  • 1
    Your second loop can be replaced with `$max = max($data); $counts = array_count_values(array_map('toScale', $data));` – Barmar Nov 16 '18 at 00:45
  • Sorry for putting my code into an image, I got some errors trying to format it properly, never happened before. Barmar's idea works and solves the problem! But still, why the above code does not work? – Lis Nov 16 '18 at 00:50
  • Do you modify `$data` between the two loops? – Barmar Nov 16 '18 at 00:51
  • My comment shouldn't fix anything, it just simplifies the code. – Barmar Nov 16 '18 at 00:51
  • That's what I thought. But somehow it does fix it. My mind is blown now – Lis Nov 16 '18 at 00:53
  • Yeah, I realized that as well. See my answer for the reason. – Barmar Nov 16 '18 at 00:58
  • 1
    How is maxNumber in toScale() determined? If it uses the global $max, it will change as you fill up the $counts array resulting in inconsistent behavior. – Kita Nov 16 '18 at 00:58

1 Answers1

3

The problem is that you're scaling your numbers differently in the two loops.

In the first loop, you're updating $max each time through the loop, and then calling toScale(data[$x]). Since toScale() depends on $max, some numbers will be scaled to a different maximum.

In the second loop, everything is scaled to the final maximum. So for the same values of $data[$a] you'll get a different toScale($data[$a]) this time.

You need to calculate the maximum of all the numbers before any calls to toScale. One way of doing this is by calculating the maximum in the loop that's generating all the random numbers:

$max = 0;
for ($a = 0; $a < $num; $a++) {
    $rand = rand();
    if ($rand > $max) {
        $max = $rand;
    }
    $data[] = $rand;
}

Or you can do it using a built-in function:

$max = max($data);
$counts = array_count_values(array_map('toScale', $data));
Barmar
  • 741,623
  • 53
  • 500
  • 612