Computer Science Canada Rolling Dice & Probability -- plz HELP!! |
Author: | Momiji [ 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:
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!! Please help me!! |
Author: | person [ Sat Apr 02, 2005 7:01 pm ] |
Post subject: | |
1) use a counter 2) you should be able to calculate the probability if ur in highschool (permutations) |
Author: | Momiji [ Sat Apr 02, 2005 9:49 pm ] |
Post subject: | alright... |
thanx for your reply 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? |
Author: | lordofall [ Sun Apr 03, 2005 1:21 am ] |
Post 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. |
Author: | Momiji [ 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.... |
Author: | person [ Sun Apr 03, 2005 10:56 am ] |
Post 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) |
Author: | Momiji [ Sun Apr 03, 2005 1:36 pm ] | ||
Post subject: | hmmm | ||
here's my table
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? |
Author: | lordofall [ Sun Apr 03, 2005 2:55 pm ] | ||||
Post subject: | Re: hmmm | ||||
Momiji wrote: here's my table
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?
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% |
Author: | Momiji [ Sun Apr 03, 2005 8:08 pm ] |
Post subject: | |
Quote: aka if u rolled 6 times and store3:=1 then actual = 16.6667% ic... 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 sry for giving you a headache.... but i really REALLY need help (as you can see, i'm not a computer person...) |
Author: | lordofall [ Sun Apr 03, 2005 9:47 pm ] | ||
Post subject: | |||
Momiji wrote: [(as you can see, i'm not a computer person...)
hehe hard for compscientist to not be computer ppl =^D okay here you go
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. |
Author: | Momiji [ Tue Apr 05, 2005 2:53 pm ] |
Post subject: | |
OH MY GOSH!!!!! the program works perfectly!! THANK YOU SOOOOOO MUCH LORDOFALL!!!!! 'n' now i know how to do the column thing.... and yeah... i will try harder lol i hope i can get better thanx4 helping me |
Author: | starlight [ Thu Apr 14, 2005 3:38 pm ] |
Post 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? |
Author: | jamonathin [ Thu Apr 14, 2005 4:30 pm ] | ||
Post subject: | |||
Definately for the if's. Use array's and for loops. (I'm guesing at this because I dont have turing here)
Array's are good, they help organize and make your program smaller |