Bug 70474 - Random number generation weak with Rnd in BASIC
Summary: Random number generation weak with Rnd in BASIC
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: BASIC (show other bugs)
Version:
(earliest affected)
4.0.5.2 release
Hardware: x86 (IA32) Windows (All)
: medium normal
Assignee: Thomas Arnhold
URL:
Whiteboard: target:4.3.0
Keywords:
Depends on:
Blocks: 90110 99809
  Show dependency treegraph
 
Reported: 2013-10-15 04:50 UTC by Johnny_M
Modified: 2016-05-12 19:01 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments
Test file (28.41 KB, application/vnd.oasis.opendocument.spreadsheet)
2013-10-15 04:50 UTC, Johnny_M
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Johnny_M 2013-10-15 04:50:18 UTC
Created attachment 87644 [details]
Test file

The RAND() function of Calc was fixed on bug 33365. Yet the same bug is present with the "Rnd" random number generation function of BASIC as well.

Even with as few records as 1000, the "random" numbers repeat themselves several times. Test document (ODS) with test macro attached.

TEST PROCEDURE:
1. Open the attached document
2. Click the "Generate" button to (re-)generate 1000 numbers. Wait for "Generation Status" to change from "Started..." to "Ready"


EXPECTED RESULTS:
2. Cell D1 shows 1.00000000 (i.e. no repetition of values in the B column)

ACTUAL RESULTS:
2. Value of D1 always bigger than 1.00000000 (between 1.02200000 and 1.04400000 on just a few tests), due to many repeated values in the B column. (Column C shows how many times a number is repeated.)


WinXP Home 32 bit, LO 4.0.5.2 release


NOTES:
- The issue does not occur on with the test file attached to bug 33365, which tests the RAND() function of Calc. To achieve comparable results, my attached file is based on that file and uses the same evaluation methods.

- Usage of the "Randomize" generator initialization function before the first "Rnd" usage does not work around the problem.

- Calc RAND() fix commit http://cgit.freedesktop.org/libreoffice/core/commit/?id=8450a99c744e9005f19173e4df35d65640bcf5c4 says that seed() "initialises the state of the global random number generator" and it "should only be called once at the start of the main programme". So, maybe the latter information should be added to the description of the "Randomize" function in LO help. Because, according to that information, additional later seeding of the random number generator would make it loose the entropy it has accumulated so far. (Assuming it does accumulate it continuously since application start.)
Comment 1 Johnny_M 2013-10-15 05:49:32 UTC
On a side note: The preliminary release notes of LO 4.2 under https://wiki.documentfoundation.org/ReleaseNotes/4.2#Calc state that a new Calc functionality has been added, to fill cell(s) with a random number(s) via Menu -> Edit -> Fill -> Random Number... I couldn't find the related implementation bug report, but it might need to be checked too, as to whether it uses the new generation method similarly to RAND() in Calc. Because there doesn't seem to be one central PRNG in LO; with different components using different implementations.
Comment 2 Johnny_M 2013-10-15 06:22:25 UTC
(In reply to comment #1)
> On a side note: The preliminary release notes of LO 4.2 under
> https://wiki.documentfoundation.org/ReleaseNotes/4.2#Calc state that a new
> Calc functionality has been added, to fill cell(s) with a random number(s)
> via Menu -> Edit -> Fill -> Random Number... I couldn't find the related
> implementation bug report, but it might need to be checked too, as to
> whether it uses the new generation method similarly to RAND() in Calc.
> Because there doesn't seem to be one central PRNG in LO; with different
> components using different implementations.

Found it, its part of bug 66477: http://cgit.freedesktop.org/libreoffice/core/commit/?id=5c05b1cabcf7f6a7f490ae6fc4bc145e75229752

----------------------------------------------

P.S.: In case it matters for this bug: I tested with an Asus EeePC netbook, with an Intel Atom N450 CPU. (Don't know if it has a built-in CPU RNG.)
Comment 3 Commit Notification 2014-05-17 12:41:39 UTC
Thomas Arnhold committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=df466d79cb126667cc9d5c108367bfa4f5ce76c8

fdo#70474: Random number generation weak with Rnd in BASIC



The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds
Affected users are encouraged to test the fix and report feedback.