Apache OpenOffice (AOO) Bugzilla – Issue 73511
Mismatch between Rnd / Randomize Help and implementation
Last modified: 2017-05-20 11:31:18 UTC
Sorry but another one thrown up by answering an oooforum topic: http://www.oooforum.org/forum/viewtopic.phtml?t=51526 According to /basic/source/runtime/methods.cxx, the Rnd() RTL function actually just does a return (double)rand() / (double)RAND_MAX. Likewise the Randomise() statement (that is with no parameter does a nSeed = (INT16)rand() However the corresponding help (helpcontent2/source/text/sbasic/shared files 03080302.xhp and 03080301.xhp) incorrectly state that (Randomize:) "If Number is omitted, the generator uses the current value of the system timer." This is patently not the case. (Rnd:) "Parameters: Expression: Any numeric expression that defines how to generate random numbers. Less than zero: Always returns the same random number. Greater than zero: Returns the next random number in the sequence. Zero: Returns the random number that was last generated. Omitted: Returns the next random number in the sequence. If the same number is passed for each successive call to the Rnd function, the same random-number sequence is generated. This is because the Expression parameter is used as a starting point for the next number." Whoever wrote the help did so on the aspiration that these both worked like VBA (since the wording seems to be a paraphrase of the VBA help). However the code does nothing like this. You need to modify one or the other so that they are consistent. If you let me know which you prefer then I can give you the appropriate changes. BTW I recommend VBA compatibility, especially since the effect of Randomize() is to do no such thing !
Thinking about this, going for full functional equivalence of the VBA rnd function is going to be a little difficult. I'm not sure what the zero and negative code would do because this involve holding [thread] static variables. Not sure how you'd easily do this in the Basic runtime model.
Terrye, sorry for the delay. Oversaw this one ... ;-) KR -> AB: Can you help?
Andreas, one side effect of tracking down issue 73457 is that I now really understand the invocation model is the basic RTE. Implementing full MS compatibility is doable here. At a minimum randomize should work as advertised. If you let me know which route you think is best: change the help to reflect what the code does or change the code to reflect what the help claims and I'll put this on my "todo list". I do think its a P4, so I am not putting a hight priority on this one. //Terry
STARTED, OOo Later
*** Issue 112296 has been marked as a duplicate of this issue. ***
While this might get fixed on the way to "Later", I'll update the Help in CWS hcshared27. File sbasic/shared/03080302.xhp
Created attachment 75960 [details] Corrected algorithm of the functions Randomize and Rnd. Corrected algorithm operates in full accordance with the description of the functions Randomize and Rnd in the Help OOo. Rnd () - Fixed a bug with range of functions - [0 .. 1). Result 1. has been excluded from output range. Processing parameters of Rnd() Rnd (-1) generates the same number belonging to the pseudo-random sequence. Rnd (0) returns the last generated number. Rnd (), Rnd (x> 0) returns the next number in the pseudorandom sequence. Randomize () initiates a new pseudo-random sequence.
Created attachment 75961 [details] Corrected algorithm of the functions Randomize and Rnd. Same patch. Fixed a missed semicolon. Sorry.
Reset assigne to the default "issues@openoffice.apache.org".