Jump to content



Photo

_DiceRoll()


  • Please log in to reply
10 replies to this topic

#1 w0uter

w0uter

    resreveR nA

  • Active Members
  • PipPipPipPipPipPip
  • 2,262 posts

Posted 22 May 2005 - 01:06 PM

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 :(

Plain Text         
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, 30 May 2005 - 08:57 PM.

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





#2 MSLx Fanboy

MSLx Fanboy

    Local $Clue = 0

  • Active Members
  • PipPipPipPipPipPip
  • 1,046 posts

Posted 23 May 2005 - 02:08 PM

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())

#3 GaryFrost

GaryFrost

    I don't need your attitude. I have one of my own

  • Developers
  • 7,854 posts

Posted 23 May 2005 - 02:36 PM

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, 23 May 2005 - 03:15 PM.

SciTE for AutoItDirections for Submitting Standard UDFs

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


#4 w0uter

w0uter

    resreveR nA

  • Active Members
  • PipPipPipPipPipPip
  • 2,262 posts

Posted 23 May 2005 - 03:38 PM

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, 23 May 2005 - 03:39 PM.

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

#5 Smed

Smed

    Polymath

  • Active Members
  • PipPipPipPip
  • 222 posts

Posted 24 May 2005 - 12:17 AM

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

#6 GaryFrost

GaryFrost

    I don't need your attitude. I have one of my own

  • Developers
  • 7,854 posts

Posted 24 May 2005 - 12:45 AM

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

Plain Text         
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, 24 May 2005 - 12:53 AM.

SciTE for AutoItDirections for Submitting Standard UDFs

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


#7 w0uter

w0uter

    resreveR nA

  • Active Members
  • PipPipPipPipPipPip
  • 2,262 posts

Posted 24 May 2005 - 10:31 PM

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, 24 May 2005 - 10:32 PM.

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

#8 /dev/null

/dev/null

    Universalist

  • MVPs
  • 2,946 posts

Posted 24 May 2005 - 11:25 PM

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, 24 May 2005 - 11:57 PM.

__________________________________________________________(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 *

#9 MSLx Fanboy

MSLx Fanboy

    Local $Clue = 0

  • Active Members
  • PipPipPipPipPipPip
  • 1,046 posts

Posted 25 May 2005 - 01:11 AM

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())

#10 w0uter

w0uter

    resreveR nA

  • Active Members
  • PipPipPipPipPipPip
  • 2,262 posts

Posted 26 May 2005 - 10:34 AM

ahh i see /dev/null. ty.

so this would be better ?

(also added some checks)

Plain Text         
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, 26 May 2005 - 10:36 AM.

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

#11 /dev/null

/dev/null

    Universalist

  • MVPs
  • 2,946 posts

Posted 26 May 2005 - 11:41 AM

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 *




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users