Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Rolling Dice & Probability -- plz HELP!!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Momiji




PostPosted: Sat Apr 02, 2005 4:25 pm   Post subject: Rolling Dice & Probability -- plz HELP!!

Hi.
I need to make a program that simulates this situation:

Say I flipped a coin 100 times. The probability of getting "head"/"tail" is 50%. But in fact, I got "head" 47 times, and "53" times.

Now, my program has to roll 2 dice "x" times depending on the user. The program has to add the two outcomes every time the dice roll (so the sum of two dice can range from 2-12 each time, right?). Then, the program has to produce a table showing the expected outcome % for getting 2..12, and the actual outcome % as well.

Here's what I have so far:

code:


randomize

var num, d1, d2, sum : int
put "how many times do you want to roll the dice?"
get num
put ""

var count, countt, try : int := 0

loop
    for i : 1 .. num
        randint (d1, 1, 6)
        randint (d2, 1, 6)
        sum := d1 + d2
        countt := count + i
        put countt, " attemp: sum of 2 dices = ", sum, "."
    end for
    exit when countt = num
end loop

put ""

var times := 0
put "outcome         actual%         expected%"
for row : 2 .. 12
    times := row + 1
    put row : 4
end for




I'm supposed to use loop somewhere.... but I have no idea how to store the outcomes or how to calculate or anything from here!! Sad
Please help me!!
Sponsor
Sponsor
Sponsor
sponsor
person




PostPosted: Sat Apr 02, 2005 7:01 pm   Post subject: (No subject)

1) use a counter

2) you should be able to calculate the probability if ur in highschool (permutations)
Momiji




PostPosted: Sat Apr 02, 2005 9:49 pm   Post subject: alright...

thanx for your reply Smile
but before i use counter, don't i have to store the #s into variables?
i don't get how i can store variables for "for" though...
and i already have one counter
do i need 1 more you mean?
lordofall




PostPosted: Sun Apr 03, 2005 1:21 am   Post subject: (No subject)

okay since i doubt ur allowed to use arrays do this.

create 11 variables (1 for every type of outcome)
then whenever u roll the die add one to the counter that represents the number.
aka

var store2,store3:int:=0
....
i roll 2 dice, i got 11

if sum = 2 then
store2:= store2+1
elsif sum= 3 then
store3:= store3+1
elsif...

ya fill in the rest of the code (aka he meant set a counter for each outcome)

heres normal probabilities for you tho i'm not sure if theres another way to do this...

[code]var a :array 2 .. 12 of int
for i : 2 .. 12
a (i) := 0
end for
for i : 1 .. 6
for j : 1 .. 6
a (i + j) := a (i + j) + 1
end for
end for
for i : 2 .. 12
put a (i)/36*100
end for[\code]

ya its with an array but basically translated to english that says, for every possible value of each dice just add their values and then print out what % of times each sum showed up.
Momiji




PostPosted: Sun Apr 03, 2005 10:00 am   Post subject: WOW

thanku so much lordofall!

do you think it's possible to use loop to store the variables?
and other than array, what else can we use to calculate probabilities? 'cuz i don't think i'm allowed to use "array" you see....
person




PostPosted: Sun Apr 03, 2005 10:56 am   Post subject: (No subject)

Quote:
and other than array, what else can we use to calculate probabilities? 'cuz i don't think i'm allowed to use "array" you see....


1) i dont see any way an array can help u calculate probability, but maybe im the only one, so if u can calculate probability by using arrays please PM me or something

2) for ur dice question, the probabilities r:

2: 1
3: 2
4: 3
5: 4
6: 5
7: 6
8: 5
9: 4
10: 3
11: 2
12: 1

(all of them r out of 36)
Momiji




PostPosted: Sun Apr 03, 2005 1:36 pm   Post subject: hmmm

here's my table
code:

var ex, ex2 : real

put "outcome         actual%         expected%"

for row : 1 .. 6
    put row + 1 : 4 ..
    for row2 : 1 .. 1
        ex := row / 36 * 100
        put ex : 35 : 2
    end for
end for
for rownum : 8 .. 12
    put rownum:4
end for
for decreasing rownew : 5 .. 1
    for rownew2 : 1 .. 1
        ex2 := rownew / 36 * 100
        put ex2 : 39 : 2
    end for
end for


there's a space between expected, and i tried to connect them but it didnt work
could someone please tell me how to show the table without the space?



PS
i tried
Quote:

var store2,store3...:int:=0

if sum = 2 then
store2:= store2+1
elsif sum= 3 then
store3:= store3+1
elsif...


but the program shows an error
for "store3:= store3+1", store3:=0, so "+1" will just make it 1, wouldn't it?
lordofall




PostPosted: Sun Apr 03, 2005 2:55 pm   Post subject: Re: hmmm

Momiji wrote:
here's my table
code:

var ex, ex2 : real

put "outcome         actual%         expected%"

for row : 1 .. 6
    put row + 1 : 4 ..
    for row2 : 1 .. 1
        ex := row / 36 * 100
        put ex : 35 : 2
    end for
end for
for rownum : 8 .. 12
    put rownum:4
end for
for decreasing rownew : 5 .. 1
    for rownew2 : 1 .. 1
        ex2 := rownew / 36 * 100
        put ex2 : 39 : 2
    end for
end for


there's a space between expected, and i tried to connect them but it didnt work
could someone please tell me how to show the table without the space?



code:
var ex, ex2 : real

put "outcome         actual%         expected%"

for row : 1 .. 11
    put row + 1 : 4 ..

    if row < 7 then
        ex := row / 36 * 100
    else
        ex := (6 - row mod 6) / 36 * 100
    end if
    put ex : 35 : 2
end for


okay i don't think anybody needs a for loop from 1 to 1 because its basically the normal code. Before your code had a space because you were printing which causes the code to go down a line.
.
now to fix that we print everything in the same loop. The numbers increased up until the 6th and after that decreased so we just used an if statement to change the formulas for the expected outcomes.

That code works fine, but when u put the actual values you'll havta decrease the distance.

Your store problem makes perfect sense. Basically store3 should be 1, and thatswh y you have to roll the dice x times before you start. Then you change it into % by dividing the store counters by the amount of times you rolled.

aka if u rolled 6 times and store3:=1 then actual = 16.6667%
Sponsor
Sponsor
Sponsor
sponsor
Momiji




PostPosted: Sun Apr 03, 2005 8:08 pm   Post subject: (No subject)

Quote:

aka if u rolled 6 times and store3:=1 then actual = 16.6667%


ic... Embarassed

look:
Quote:

%------------------------------------------------------------------------
var num, d1, d2, sum1 : int
put "how many times do you want to roll the dice?"
get num
put ""
%------------------------------------------------------------------------
randomize
var count : int := 0
var s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12 : int := 0
for i : 1 .. num
randint (d1, 1, 6)
randint (d2, 1, 6)
sum1 := d1 + d2
count := count + 1
delay (200)
put count, " attemp: sum of 2 dice = ", sum1
if sum1 = 2 then
s2 := s2 + 1
elsif sum1 = 3 then
s3 := s3 + 1
elsif sum1 = 3 then
s4 := s4 + 1
elsif sum1 = 3 then
s5 := s5 + 1
elsif sum1 = 3 then
s6 := s6 + 1
elsif sum1 = 3 then
s7 := s7 + 1
elsif sum1 = 3 then
s8 := s8 + 1
elsif sum1 = 3 then
s9 := s9 + 1
elsif sum1 = 3 then
s10 := s10 + 1
elsif sum1 = 3 then
s11 := s11 + 1
else
s12 := s12 + 1
end if
end for
put ""
%------------------------------------------------------------------------
var ex, ex2 : real
put "outcome actual% expected%"
for row : 1 .. 11
put row + 1 : 4 ..

if row < 7 then
ex := row / 36 * 100
else
ex := (6 - row mod 6) / 36 * 100
end if
put ex : 35 : 2
end for

put s2 / num * 100
put s3 / num * 100
put s4 / num * 100
put s5 / num * 100
put s6 / num * 100
put s7 / num * 100
put s8 / num * 100
put s9 / num * 100
put s10 / num * 100
put s11 / num * 100
put s12 / num * 100



honestly, i dunno what i've done to it!! :'(
i thought it'd show actual%...
and again, it's out of the column range Confused

sry for giving you a headache.... Embarassed
but i really REALLY need help (as you can see, i'm not a computer person...)
lordofall




PostPosted: Sun Apr 03, 2005 9:47 pm   Post subject: (No subject)

Momiji wrote:
[(as you can see, i'm not a computer person...)

hehe Razz hard for compscientist to not be computer ppl =^D

okay here you go
code:
%------------------------------------------------------------------------
var num, d1, d2, sum1 : int
put "how many times do you want to roll the dice?"
get num
put ""
%------------------------------------------------------------------------
randomize
var count : int := 0
var s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12 : int := 0
for i : 1 .. num
    randint (d1, 1, 6)
    randint (d2, 1, 6)
    sum1 := d1 + d2
    count := count + 1
    delay (200)
    put count, " attemp: sum of 2 dice = ", sum1
    if sum1 = 2 then
        s2 := s2 + 1
    elsif sum1 = 3 then
        s3 := s3 + 1
    elsif sum1 = 4 then
        s4 := s4 + 1
    elsif sum1 = 5 then
        s5 := s5 + 1
    elsif sum1 = 6 then
        s6 := s6 + 1
    elsif sum1 = 7 then
        s7 := s7 + 1
    elsif sum1 = 8 then
        s8 := s8 + 1
    elsif sum1 = 9 then
        s9 := s9 + 1
    elsif sum1 = 10 then
        s10 := s10 + 1
    elsif sum1 = 11 then
        s11 := s11 + 1
    else
        s12 := s12 + 1
    end if
end for
put ""
%------------------------------------------------------------------------
var ex, ex2 : real
put "outcome actual% expected%"
for row : 1 .. 11
    put row + 1 : 4 ..
    if row = 1 then
        put s2 / num * 100 :12 : 2 ..
    elsif row = 2 then
        put s3 / num * 100 :12 : 2 ..
    elsif row = 3 then
        put s4 / num * 100 :12 : 2 ..
    elsif row = 4 then
        put s5 / num * 100 :12 : 2 ..
    elsif row = 5 then
        put s6 / num * 100 :12 : 2 ..
    elsif row = 6 then
        put s7 / num * 100 :12 : 2 ..
    elsif row = 7 then
        put s8 / num * 100 :12 : 2 ..
    elsif row = 8 then
        put s9 / num * 100 :12 : 2 ..
    elsif row = 9 then
        put s10 / num * 100 :12 : 2 ..
    elsif row = 10 then
        put s11 / num * 100 :12 : 2 ..
    elsif row = 11 then
        put s12 / num * 100 :12 : 2 ..
    end if

    if row < 7 then
        ex := row / 36 * 100
    else
        ex := (6 - row mod 6) / 36 * 100
    end if
    put ex : 12 : 2
end for


here were ur issues
issue 1) ur statement to collect the sums had all if statements check "3" so if 3 came up sum3 went up and the rest of the counters would never execute (because its an if statement and once a condition is true it executes the loop and exits the statement)
(if sum1 = 3 then
s3 := s3 + 1
elsif sum1 = 3 then
s4:= s4+1
else...)

issue 2) Umm ur printing out each roll which slows the program down a lot, see if your allowed to take that out

issue 3) you were putting the Actual results in a seperate for loop, and i explained earler that after every put statement the program went to the next line. So in this version it puts out the entire line at once
(result, actual %, and expected%)
stopping it from going to the next line.

and dw compsci takes practice and you'll get better.
Momiji




PostPosted: Tue Apr 05, 2005 2:53 pm   Post subject: (No subject)

OH
MY
GOSH!!!!!
the program works perfectly!!
THANK YOU SOOOOOO MUCH LORDOFALL!!!!! Very Happy
'n' now i know how to do the column thing.... Embarassed

and yeah... i will try harder lol
i hope i can get better Smile

thanx4 helping me Surprised
starlight




PostPosted: Thu Apr 14, 2005 3:38 pm   Post subject: (No subject)

I was thinking if there are any other ways to do the programe, other than using array and so many if statement. using counted loop or someting? any suggestion?
jamonathin




PostPosted: Thu Apr 14, 2005 4:30 pm   Post subject: (No subject)

Definately for the if's. Use array's and for loops. (I'm guesing at this because I dont have turing here)
code:
var s : array 2 .. 12 of int
%for first if statement use for loop inside for loop
for i: 2 . . 12
        if sum1 = i then
              s(i)+=1
        end if
end for

%for second if statement use for loop inside for loop
for q: 1 . . 11
        if row = q then
              put s(q + 1) / num * 100 :12 : 2 ..
        end if
end for
 

Array's are good, they help organize and make your program smaller
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: