HP Forums
Numworks - strange random numbers - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not remotely HP Calculators (/forum-9.html)
+--- Thread: Numworks - strange random numbers (/thread-19614.html)



Numworks - strange random numbers - LowTech - 03-04-2023 05:10 PM

Hi,

playing around with the latest Numworks app on Android, I noticed a strange behavior of the random() function.

When tested with many pairs of random numbers between 0 and 1, min(random#, random#) should converge to 1/3. And max(random#, random#) to 2/3. However, the Numworks app delivers ~1/2 for both min() and max() instead.

My HP Prime confirms the expected results ~1/3 and ~2/3 with its random numbers.

Code:
sum(k, 1, 10000, min({random(), random()})) / 10000

Am I doing something wrong? What do you get on the real Numworks calculator?


P.S. kind of wired is too, that 10001 steps are the maximum for Sum()


RE: Numworks - strange random numbers - Albert Chan - 03-04-2023 08:02 PM

Tried on https://www.numworks.com/simulator/, it make no sense.

> min({0.5, random()})

0.5
0.9116125675
0.5
0.5
0.5140082607
0.06554496133


RE: Numworks - strange random numbers - toml_12953 - 03-05-2023 12:00 AM

(03-04-2023 05:10 PM)LowTech Wrote:  Hi,

playing around with the latest Numworks app on Android, I noticed a strange behavior of the random() function.

When tested with many pairs of random numbers between 0 and 1, min(random#, random#) should converge to 1/3. And max(random#, random#) to 2/3. However, the Numworks app delivers ~1/2 for both min() and max() instead.

My HP Prime confirms the expected results ~1/3 and ~2/3 with its random numbers.

Code:
sum(k, 1, 10000, min({random(), random()})) / 10000

Am I doing something wrong? What do you get on the real Numworks calculator?


P.S. kind of wired is too, that 10001 steps are the maximum for Sum()

The real Numworks gets the same 1/2. Numworks seems to use a normal distribution where numbers around .5 are chosen more frequently than numbers approaching 0 or .999999 The probability of a particular random number in other machines is about equal throughout the range.


RE: Numworks - strange random numbers - LowTech - 03-05-2023 12:29 PM

(03-05-2023 12:00 AM)toml_12953 Wrote:  The real Numworks gets the same 1/2. Numworks seems to use a normal distribution where numbers around .5 are chosen more frequently than numbers approaching 0 or .999999 The probability of a particular random number in other machines is about equal throughout the range.

Quite possible, but can't find any notes about it in the manual. Anyway, pretty unusual.

Are other calculators known to behave like this?


RE: Numworks - strange random numbers - Albert Chan - 03-05-2023 01:17 PM

There is a bug with list containing random numbers.
The list cannot be sorted, getting minimum or maximum.

NUMWORKS> sort({random(), random()})

{0.03173426865, 0.1906006013}
{0.9829388464, 0.1990888752}      // ?
{0.5004268227, 0.4287105828}      // ?


RE: Numworks - strange random numbers - EdS2 - 03-05-2023 02:32 PM

(03-04-2023 08:02 PM)Albert Chan Wrote:  Tried on https://www.numworks.com/simulator/, it make no sense.

> min({0.5, random()})

0.5
0.9116125675
0.5
0.5
0.5140082607
0.06554496133

Could this be a case of min() evaluating the arguments more than once?


RE: Numworks - strange random numbers - ijabbott - 03-06-2023 08:50 PM

(03-05-2023 02:32 PM)EdS2 Wrote:  
(03-04-2023 08:02 PM)Albert Chan Wrote:  Tried on https://www.numworks.com/simulator/, it make no sense.

> min({0.5, random()})

0.5
0.9116125675
0.5
0.5
0.5140082607
0.06554496133

Could this be a case of min() evaluating the arguments more than once?

I am thinking along the same lines. Perhaps it evaluates each expression in the list, determines which one produced the minimum value, and evaluated that expression again.

That would also explain why the average value of min({random(), random()}) would be 0.5. It wouldn't matter what the first two random() calls produced. One of those expressions would be the minimum, so it would evaluate that expression again, i.e. call random() again for the final result.


RE: Numworks - strange random numbers - Albert Chan - 03-06-2023 11:27 PM

(03-23-2020 04:34 PM)Albert Chan Wrote:  LN(2) = 2 * probability of integer part of RND/RND is odd

> sum(rem(floor(random()/random()),2),k,1,10000) / 5000

2
0
2
...

We expected around log(2) = 0.6931, but getting 0 or 2 only.
It seems argument inside sum is evaluated only once.

random(), and its cousins, should not be treated as normal function.
It should never be optimized away.

Unfortunately, it seems Numworks fix is like whack-a-mole game.
Example, from github fixed issues: random()-random() gets simplified to 0

Above expression, replacing floor(x) = round(x-0.5) work.

> sum(rem(round(random()/random()-0.5),2),k,1,10000) / 5000

0.7034
0.6998
0.6938
...


RE: Numworks - strange random numbers - toml_12953 - 03-07-2023 04:47 PM

(03-06-2023 11:27 PM)Albert Chan Wrote:  Unfortunately, it seems Numworks fix is like whack-a-mole game.
Example, from github fixed issues: random()-random() gets simplified to 0

When I print random()-random() I get a number. My OS version is 20.3.0.


RE: Numworks - strange random numbers - Albert Chan - 03-07-2023 07:41 PM

(03-07-2023 04:47 PM)toml_12953 Wrote:  When I print random()-random() I get a number. My OS version is 20.3.0.

I found this in NumWorks github fixed issue. However, the bug is not truly fixed.

> abs(random()-random())
-0.5887471884         // negative ?

> (random()-random())^2
-0.6283539792         // negative ?


RE: Numworks - strange random numbers - toml_12953 - 03-07-2023 11:16 PM

(03-07-2023 07:41 PM)Albert Chan Wrote:  
(03-07-2023 04:47 PM)toml_12953 Wrote:  When I print random()-random() I get a number. My OS version is 20.3.0.

I found this in NumWorks github fixed issue. However, the bug is not truly fixed.

> abs(random()-random())
-0.5887471884         // negative ?

> (random()-random())^2
-0.6283539792         // negative ?

Yup. I just upgraded to 20.4.0 and sometimes (random()-random())^2 still comes up negative. Imagine that. random can return imaginary numbers! Big Grin