Sign in to follow this  
Followers 0
w0uter

_DiceRoll()

11 posts in this topic

#1 ·  Posted (edited)

Made a diceroll func in java.

Wonderd if it was harder in autoit.

and it was SOOO easy.

but i still decided to post it :(

Func _DiceRoll($i_Dices = 1, $i_Sides = 6)
    
;check for valid numbers
    If Not IsInt($i_Dices) Or Not IsInt($i_Sides) Or Round($i_Dices) <> $i_Dices Or Round($i_Sides) <> $i_Sides Then
        SetError(1)
        Return 0
    EndIf
    
;filter out technical impossibilitys
    If $i_Dices <= 0 Or $i_Sides <= 3 Then
        SetError(2)
        Return 0
    EndIf
    
;add the buffer
    Local $i_rnd = 0
    
;roll the dices and add em to the buffer
    For $i_count = 1 To $i_Dices
        $i_rnd += Random(1, $i_Sides, 1)
    Next
    
;return the buffer
    Return $i_rnd
    
EndFunc;==>_DiceRoll
Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites



Is there a reason why you multiply $i_Dices by 1 in the first argument in the Random() func


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

seemed useless to me

$a_dice = _DiceRoll(2)
$s_text = ""
For $i = 1 To $a_dice[0]
    $s_text = $s_text & "Die #" & $i & ": " & $a_dice[$i] & @LF
Next
$s_text = $s_text & "Total: " & $a_dice[UBound($a_dice) - 1]
MsgBox(0,"Rolled",$s_text)

Func _DiceRoll($i_Dices = 1, $i_Sides = 6)
    Dim $a_dices[$i_Dices  + 2], $i
    $a_dices[0] = $i_Dices
    For $i = 1 to $a_dices[0]
        $a_dices[$i] = Random(1, $i_Sides, 1)
        $a_dices[$i_Dices + 1] = $a_dices[$i_Dices + 1] + $a_dices[$i]
    Next
    Return $a_dices
EndFunc
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Is there a reason why you multiply $i_Dices by 1 in the first argument in the Random() func

<{POST_SNAPBACK}>

no not really. first it had 1.

but then i added multiple dices.

and forgot to remove the 1.

ty for pointing that out :(

Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites

gafrost corrected for it, but he didn't point out that yours will return each possible value with a linear probability rather than the nice bell curve you expect from rolling multiple dice. I.e. if you roll 2, 6-sided dice, you are more likely to get a 7 than a 2 or a 12.


601DisengageEnd Program

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

took about 30 seconds to put the below together, probably need more items on the wheel, lower the chances, but see lots of potential for the function for making games like below, even the old game of yatzee

dim $slots[7]
$slots[1] = 'cherry'
$slots[2] = 'bananna'
$slots[3] = 'coin'
$slots[4] = 'watermelon'
$slots[5] = 'lemon'
$slots[6] = 'Lucky 7'
$a_dice = _DiceRoll(3)
$s_text = ""
For $i = 1 To $a_dice[0]
    $s_text = $s_text & "Slot #" & $i & ": " & $slots[$a_dice[$i]] & @LF
Next
MsgBox(0,"1 armed bandit",$s_text)

Func _DiceRoll($i_Dices = 1, $i_Sides = 6)
    Dim $a_dices[$i_Dices  + 2], $i
    $a_dices[0] = $i_Dices
    For $i = 1 to $a_dices[0]
        $a_dices[$i] = Random(1, $i_Sides, 1)
        $a_dices[$i_Dices + 1] = $a_dices[$i_Dices + 1] + $a_dices[$i]
    Next
    Return $a_dices
EndFunc
Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

gafrost corrected for it, but he didn't point out that yours will return each possible value with a linear probability rather than the nice bell curve you expect from rolling multiple dice.  I.e. if you roll 2, 6-sided dice, you are more likely to get a 7 than a 2 or a 12.

<{POST_SNAPBACK}>

i think this is incorrect. since dices have the same chances for evry side. Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

i think this is incorrect. since dices have the same chances for evry side.

<{POST_SNAPBACK}>

Nope, it is correct. Dices have the same chances if you throw them independently but not if you add the values of each dice, because then you have dependent probabilities.

See the following numbers for a "visual" proof ;-))

1+1 = 2

1+2 = 3

1+3 = 4

1+4 = 5

1+5 = 6

1+6 = 7

2+1 = 3

2+2 = 4

2+3 = 5

2+4 = 6

2+5 = 7

2+6 = 8

3+1 = 4

3+2 = 5

3+3 = 6

3+4 = 7

3+5 = 8

3+6 = 9

4+1 = 5

4+2 = 6

4+3 = 7

4+4 = 8

4+5 = 9

4+6 = 10

5+1 = 6

5+2 = 7

5+3 = 8

5+4 = 9

5+5 = 10

5+6 = 11

6+1 = 7

6+2 = 8

6+3 = 9

6+4 = 10

6+5 = 11

6+6 = 12

value=2 : count= 1

value=3 : count= 2

value=4 : count= 3

value=5 : count= 4

value=6 : count= 5

value=7 : count= 6

value=8 : count= 5

value=9 : count= 4

value=10 : count= 3

value=11 : count= 2

value=12 : count= 1

There are simply more combinations to get a 7 (6/36) than a 2 or 12 (1/36), so the chances to throw a seven are much higher.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

God bless Mutually-Exclusive probabilities. Hehe, just covered that a few weeks ago in PreCalc, I miss them now that we did limits of functions and such :(


Writing AutoIt scripts since
_DateAdd("d", -2, _NowCalcDate())

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

ahh i see /dev/null. ty.

so this would be better ?

(also added some checks)

Func _DiceRoll($i_Dices = 1, $i_Sides = 6)
    
;check for valid numbers
    If Not IsInt($i_Dices) Or Not IsInt($i_Sides) Or Round($i_Dices) <> $i_Dices Or Round($i_Sides) <> $i_Sides Then
        SetError(1)
        Return 0
    EndIf
    
;filter out technical impossibilitys
    If $i_Dices <= 0 Or $i_Sides <= 3 Then
        SetError(2)
        Return 0
    EndIf
    
;add the buffer
    Local $i_rnd = 0
    
;roll the dices and add em to the buffer
    For $i_count = 1 To $i_Dices
        $i_rnd += Random(1, $i_Sides, 1)
    Next
    
;return the buffer
    Return $i_rnd
    
EndFunc ;==>_DiceRoll
Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Share this post


Link to post
Share on other sites

ahh i see /dev/null. ty.

so this would be better ?

Yes, running your func 10.000 times gives:

value=2 count=259

value=3 count=525

value=4 count=834

value=5 count=1138

value=6 count=1443

value=7 count=1671

value=8 count=1323

value=9 count=1134

value=10 count=819

value=11 count=530

value=12 count=324

See also the post of gafrost in this thread.

Cheers

Kurt


__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0