LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Bug in 'Random Number (Range) U64' - Values can Exceed Upper Bound

Solved!
Go to solution

Hello,

 

I noticed that there's a bug in Random Number (Range) U64. When the difference between the Upper and Lower Bound exceeds 4294967295 (2^32 - 1), it's possible for the generated value to exceed the upper bound.

 

The VI splits the difference into two U32s, generates two random U32s, and then joins them back to make a U64. The bug comes from the upper bound of the random lo(x) being hardcoded to (2^32 - 1).

 

ng1902_0-1647450603867.png

 

 

When it splits the U64, it uses the hi() terminal to randomly determine the hi() value of the random number it creates. However, if the random value for hi() equals the actual value for hi(), it's possible for the randomly generated number to exceed the bounds.

 

To easily demonstrate the bug, enter an upper limit of 4294967296 (2^32). Run it a few times and you'll quickly see it exceeding the upper bound half the time it runs.

 

ng1902_1-1647451210781.png

 

 

Random Range U64 - with indicators.png

 

ng1902_2-1647451891009.png

 

Whenever the random hi(x) word equals 1, the value it outputs already equals 2^32 = 4294967296. If the random lo(x) equals anything other than 0, it will exceed the upper bound of 2^32. Specifying an upper bound of 2^32 actually gives an upper bound of (2^33 - 1).

 

4294967296 is split into:

  hi(x) = 1

  lo(x) = 0

 

If you were to create a new value using

  hi(x) = 1

  lo(x) = ____

 

A lo(x) of anything other than 0 will exceed the original value.

 

 

The random lo(x) long needs to take the random hi(x) value into consideration. When the random hi(x) value equals the hi(x) value of the difference, the random lo(x) value should be limited to the lo(x) of the difference.

 

That is, something along the lines of:

 

Random Range U64 - Low Bound Consideration.png

 

ng1902_3-1647451948699.png

 

 

0 Kudos
Message 1 of 6
(1,212 Views)

Actually, simply limiting the upper-bound of random lo(x) isn't enough.

 

Running with an upper-bound of 2^32 means that half the time, the random hi(x) equals 1. So half the time the value that gets returned is 2^32.

 

ng1902_0-1647452864626.png

 

0 Kudos
Message 2 of 6
(1,204 Views)

So the comment in 'Random Number (Range)' says it splits things into 32-bit integers because at the high ranges of U64, DBLs start to lose precision and some numbers might get skipped.

 

DBLs are listed as having a precision of 15 digits, and U64s can go up to 20 digits.

 

An idea instead of splitting the U64 into two U32s... if the value exceeds 2^32, subtract a big value from it (in the screenshot I chose 2^31). Multiply the range and this subtraction by the same random number, convert both to U64, then add them together. The small number should maintain random precision on the low-value digits, and it can no longer exceed the value / upper bound.

 

ng1902_1-1647459435103.png

 

 

I think this would let numbers keep their low-decimal precision while also removing the bug where U64s can exceed the limit.

0 Kudos
Message 3 of 6
(1,178 Views)
Solution
Accepted by topic author ng1902

@ng1902 wrote:

Hello,

 

I noticed that there's a bug in Random Number (Range) U64. ... it's possible for the generated value to exceed the upper bound.


It is unclear if you think you discovered the bug, or that you discovered the LV known issues page 😉

 

Frozen_0-1647464889277.png

 

---------------------------------------------
Certified LabVIEW Developer (CLD)
There are two ways to tell somebody thanks: Kudos and Marked Solutions
0 Kudos
Message 4 of 6
(1,164 Views)

@Frozen wrote:

@ng1902 wrote:

Hello,

 

I noticed that there's a bug in Random Number (Range) U64. ... it's possible for the generated value to exceed the upper bound.


It is unclear if you think you discovered the bug, or that you discovered the LV known issues page 😉

 

Frozen_0-1647464889277.png

 


You have to admire the detective work.  🙂

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 5 of 6
(1,159 Views)

D'oh!

 

I searched google & the forums for things like 'random number range bug', and the known issues list didn't show up in the results.

 

Oh well, it was fun investigating!

Message 6 of 6
(1,112 Views)