Issue 73511 - Mismatch between Rnd / Randomize Help and implementation
Summary: Mismatch between Rnd / Randomize Help and implementation
Status: ACCEPTED
Alias: None
Product: General
Classification: Code
Component: scripting (show other issues)
Version: OOo 2.1
Hardware: All Windows XP
: P4 Trivial with 1 vote (vote)
Target Milestone: AOO Later
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords:
: 112296 (view as issue list)
Depends on:
Blocks:
 
Reported: 2007-01-16 11:10 UTC by terrye
Modified: 2017-05-20 11:31 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments
Corrected algorithm of the functions Randomize and Rnd. (1.40 KB, patch)
2011-03-01 18:38 UTC, garbuz
no flags Details | Diff
Corrected algorithm of the functions Randomize and Rnd. (1.40 KB, patch)
2011-03-01 18:58 UTC, garbuz
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description terrye 2007-01-16 11:10:49 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 !
Comment 1 terrye 2007-01-18 02:20:20 UTC
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.
Comment 2 kay.ramme 2007-02-05 13:40:57 UTC
Terrye, sorry for the delay. Oversaw this one ... ;-)


KR -> AB: Can you help?
Comment 3 terrye 2007-02-08 15:33:44 UTC
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     
Comment 4 ab 2007-02-20 14:28:56 UTC
STARTED, OOo Later
Comment 5 Uwe Fischer 2010-06-14 11:51:09 UTC
*** Issue 112296 has been marked as a duplicate of this issue. ***
Comment 6 Uwe Fischer 2010-06-14 11:58:08 UTC
While this might get fixed on the way to "Later", I'll update the Help in CWS
hcshared27. 
File sbasic/shared/03080302.xhp
Comment 7 garbuz 2011-03-01 18:38:01 UTC
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.
Comment 8 garbuz 2011-03-01 18:58:43 UTC
Created attachment 75961 [details]
Corrected algorithm of the functions Randomize and Rnd.

Same patch. Fixed a missed semicolon. Sorry.
Comment 9 Marcus 2017-05-20 11:31:18 UTC
Reset assigne to the default "issues@openoffice.apache.org".