[Solved] Function rnd()*20 = 20, sometimes

Creating a macro - Writing a Script - Using the API

[Solved] Function rnd()*20 = 20, sometimes

Postby BlueSkyBlackBird » Fri May 05, 2017 5:20 pm

Hello everyone,
yesterday I tried to implement a dice-roller in basic and use it as a macro. That worked. Then I wanted to see how "random" the rnd() function actually is. To do that I created a function that generates a number of rolls (with a fixed die-size), writes the result into an array. Since I use a 20-sided die, I assumed that an array with 20 entries would suffice. Yet I was mistaken as I encountered out of bounds accesses. Looking deeper into this I found that my function would produce values in the range of (1:21) which was clearly not intended. However, I fail to see what this is caused by.

Code: Select all   Expand viewCollapse view
function fnf()
Randomize
Dim value as Integer
Dim values(19) as Long

For I = 1 to 100000
   value = CInt(Int(rnd()* 20)+1)
   values(value-1)  = values(value-1) + 1
Next I

rstring = ""
For I=0 to 19
   rstring = rstring & CStr(I+1) & ":" & CStr(values(I)) & ",  "
Next I

MsgBox rstring
End function


After testing I found that rnd()*20 results in values of 20, yet rnd() itself is never 1. The values which result in 21 seem random to me: I found a 0.98.. or a 0.67 as return value for rnd(). According to the doc, rnd() returns a Single which should be able to handle multiplication without an overflow. I assume this error is caused by some missing type conversion I simply did not do.
Last edited by BlueSkyBlackBird on Sat May 06, 2017 1:43 am, edited 3 times in total.
OpenOffice 3.1.3 Win7 x64
BlueSkyBlackBird
 
Posts: 3
Joined: Fri May 05, 2017 5:02 pm

Re: Function rnd()*20 = 20, sometimes

Postby UnklDonald418 » Fri May 05, 2017 11:58 pm

yet rnd() itself is never 1

That is incorrect, rnd() does return the value 1, thus the 21 you are seeing in your results.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the subject line
Apache OpenOffice 4.1.3 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 535
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Function rnd()*20 = 20, sometimes

Postby BlueSkyBlackBird » Sat May 06, 2017 12:07 am

Again: Not according to doc. Also the 21 is produced by values other than a 1 for rnd().
OpenOffice 3.1.3 Win7 x64
BlueSkyBlackBird
 
Posts: 3
Joined: Fri May 05, 2017 5:02 pm

Re: Function rnd()*20 = 20, sometimes

Postby UnklDonald418 » Sat May 06, 2017 1:13 am

Not according to doc.

Not sure what doc you are referring to, but it appears to be wrong. The rnd() function returns both 0 and 1. The following code will verify that though rare they are both returned.
Code: Select all   Expand viewCollapse view
REM  *****  BASIC  *****

    function fnf()
    Randomize
    Dim value as Integer
    Dim values(20) as Long
    Dim raw
    Dim I
    Dim test0 as integer
    Dim test1 as integer

   test0 = 0 : test1 = 0
    For I = 1 to 100000
       raw = rnd()
       if raw = 1 then
         test1 = test1 + 1
       end if
       if raw = 0 then
         test0 = test0 + 1
       end if
       value = CInt(Int(raw * 20) + 1)
       values(value-1)  = values(value-1) + 1
    Next I
   print test0 & " 0's returned : " & test1 &  " 1's returned"
    rstring = ""
    For I=0 to 20
       rstring = rstring & CStr(I+1) & ":" & CStr(values(I)) & ",  "
    Next I

    MsgBox rstring
    End function
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the subject line
Apache OpenOffice 4.1.3 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 535
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Function rnd()*20 = 20, sometimes

Postby BlueSkyBlackBird » Sat May 06, 2017 1:42 am

It looks like you were right. Testing this made also clear what the problem was. The 21 was caused by the rnd() returning one. I also tested this using a variable called rnd, coming from a java background, I did not see a problem with that. However here It seems that one can call functions without using the braces. So what really happen is that every call of rnd as variable called rnd() instead and gave me a new value.

I thank you very much and I think I can change this to [solved].
OpenOffice 3.1.3 Win7 x64
BlueSkyBlackBird
 
Posts: 3
Joined: Fri May 05, 2017 5:02 pm

Re: [Solved] Function rnd()*20 = 20, sometimes

Postby UnklDonald418 » Sat May 06, 2017 2:08 am

Yes, function names are reserved in Basic. When there are no arguments being passed the braces are optional. Leaving them off doesn't help much with readability.
I did a little more testing and it looks like Int() and CInt() round anything below .05 to 0.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the subject line
Apache OpenOffice 4.1.3 - Windows 10 Professional
UnklDonald418
Volunteer
 
Posts: 535
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: [Solved] Function rnd()*20 = 20, sometimes

Postby Villeroy » Sun May 07, 2017 7:12 pm

You are not restricted to StarBasic. I recommend Python as macro language. The office suite comes with a Python runtime and a bridge between the Python language and the UNO API
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Villeroy
Volunteer
 
Posts: 24253
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to Macros and UNO API

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests