You are not logged in.
Pages: 1
I've been trying to write a script to calculate the odds of getting a particular number when rolling a pair of 10-sided dice and adding them together. The ruby script I've been trying to write simply does not work. I don't know if I'm overlooking something or stumbled into a bug with the ruby interpreter. Could someone take a look at it and tell me if anything's wrong?
Here's the offending section of the script:
$ary = Array.new(20, 0)
$x, $y, $z = 0, 0, 0
10.times do
$x += 1
10.times do
$y += 1
$z = $x + $y
$ary[$z] += 1 #This is the line causing problems
end
end
And here's the error message I'm getting:
Dice.rb:9: undefined method `+' for nil:NilClass (NoMethodError)
from Dice.rb:6:in `times'
from Dice.rb:6
from Dice.rb:4:in `times'
from Dice.rb:4
Offline
I don't /speak/ ruby, but looking at your code it seems that `y` will take all values between 1 and 100 thus making z take all values between 2 and 110. This is probably problematic since it seems to me your array only has 20 elements.
Last edited by sniffles (2008-06-25 13:46:31)
Offline
$ary = Array.new(20, 0)
$x, $y, $z = 0, 0, 0
10.times do
$x += 1
$y += 1
$z = $x + $y
$ary[$z] += 1 #This is the line causing problems
end
try this
Offline
Is there a particular reason you're using global variables in Ruby (prefixed with '$')? Variable names with no prefix are local to whatever scope they are in.
Last edited by Jessehk (2008-06-25 13:56:04)
Offline
I don't /speak/ ruby, but looking at your code it seems that `y` will take all values between 1 and 100 thus making z take all values between 2 and 110. This is probably problematic since it seems to me your array only has 20 elements.
Now I see the problem. I had forgotten to reset `y` to 0 at the end of each iteration of `x`. Honestly, I should have used a for loop, and I'm not sure why.
And the global variables were just being used because I wasn't sure.
Offline
BTW, is this a ruby task or a math task? If it's a math task, it's not so difficult as to need ruby.
Offline
You're also "wasting" 2 elements in your array as you will never get z = 0 or 1. If these is an academic exercise i.e. you are doing it for practice not for purpose, you might consider creating a hash instead of an array with keys 2 through 20 to refer to each result.
Offline
Well, I was just being lazy, because I didn't want to do math on all 100 combinations. Shortly after my initial post I opened up OpenOffice.org and used a table to figure it out. Not much math was needed, because there was a very noticable linear pattern (line 1 was 2, 3, 4, 5, 6, 7, 8, 9, 10, 11; line 2 was 3, 4, 5, 6, 7, 8, 9, 10, 11, 12; and so on) The odds were also had a very linear pattern:
2: 1%
3: 2%
4: 3%
5: 4%
6: 5%
7: 6%
8: 7%
9: 8%
10: 9%
11: 10%
12: 9%
13: 8%
14: 7%
15: 6%
16: 5%
17: 4%
18: 3%
19: 2%
20: 1%
Offline
You're also "wasting" 2 elements in your array as you will never get z = 0 or 1. If these is an academic exercise i.e. you are doing it for practice not for purpose, you might consider creating a hash instead of an array with keys 2 through 20 to refer to each result.
I was doing it for a purpose, but wasted elements are not a concern for me since this was just a script I would be using only once. I'm in the process of designing a tabletop RPG. One of the mechanics in the game is the use of 2d10, which are rolled for resolving actions. I needed to know the odds, as they would affect the game mechanics.
Offline
Note: you don't have "100 cases". You have just 19, because you are only ref. to the sum, not to individual dice numbers. So it's rather:
2: 1/19 * 100 %
3: 2/19 * 100 %
4: 3 / 19 * 100 %
etc.
There are 19 "possible cases" (outcomes): mainly the sum is one of 2, 3, ..., 20. The probability to get the sum "2" is 1 / 19 (one favorable case: 1-1 on the dices); the probability to get the sum "3" is 2 / 19 (two favorable cases: 1-2 or 2-1 on the dices); etc.
Last edited by sniffles (2008-06-25 17:31:11)
Offline
Pages: 1