problem with dice rolling program

ok so i got this program where i need to roll n number of dice
i got this idea about it but the thing is it seems to be printing out
2 of the same numbers one after the other almost always.
i tried all things i know to no avail.
for example instead of rolling smth like
1 4 2
usually the output is
1 1 4
or
2 2 5
or smth like that
i know that can happen in real dice
but this happens almost always
the chances of that are like 0.so it must be a problem with my code
thanks for help :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 #include <iostream>
#include <ctime>
#include <stdlib.h>
#include <limits>
using namespace std;

int DiceRoll();


int main(){

    srand(time(0));
    int n;
cout<<"kolku pati sakas da gi svrtis kockite?"<<endl;
cin>>n;
for(int i=0;i<n;i++){
    cout<<"vrtenje broj "<<i<<": ";
    cout<<DiceRoll()<<endl;


}
  system("PAUSE");
}
int DiceRoll(){
int Roll;
for(int i=0;i<15;i++){}{
Roll=rand()%6+1;
}
return Roll;


}

also i know system('PAUSE") shouldn't be used
but since this was quick and dirty i couldn't bother to put in anything else
Last edited on
Of course there's a 1/6 chance that a number will be followed up by the same number. Sure you're not overestimating the effect? Code looks ok to me.
The code ran perfectly on ma machine with no irregular repetition.
it seems to run ok on my machine now aswell.
i guess it was correct.
alright now that thats sorted
i have another question.
the assignment also says to calculate the chance of that sequence happening and print it as a number from 0-1.
this isn't a programming problem,i don't understand how that works.
can someone give me an example like
what are the chances of the numbers 1,1,3 being rolled.
ik this is more a math prob than anything
why do you have a for loop that runs 14 times in the roll function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <limits>
using namespace std;

int DiceRoll();


int main(){
    
  srand(time(0));
    
    int n;
    
    cout<<"How many times would you like to roll the Dice?"<<endl;
    cin>>n;
    
    for(int i=0;i<n;i++){
        
        cout<<"Dice Roll "<<i+1<<": ";
        
        cout<<DiceRoll()<<endl;
        
    }
}
int DiceRoll(){

        return rand()%6+1;
    
}
Last edited on
it seems to run ok on my machine now aswell.
i guess it was correct.
alright now that thats sorted
i have another question.
the assignment also says to calculate the chance of that sequence happening and print it as a number from 0-1.
this isn't a programming problem,i don't understand how that works.
can someone give me an example like
what are the chances of the numbers 1,1,3 being rolled.
ik this is more a math prob than anything



Think about coin flips instead of dice rolls for the moment. What is the chance of flipping heads (H)? What is the probably of flipping H then tails (T)? What about HTHTHHTHHHHTTTTHHH? Think about it for a bit, and then extend this to the dice by realizing that instead of two possible outcomes for each roll there are six.
Since when did this become a math forum.

Ok. First, this is Conditional Probability .

Conditional prob is the prob of A given B has occurred. Denoted P(A|B).

P(A|B) = P(AnB)/P(B).

Back to our program.

The sequence 1 ,1, 3.

Firstly, you and i know that the prob of A = 1 is P(A) = 1/6.
On the second roll, we want the prob of B = 1 given that A has occurred right?
The prob P(B|A) = P(A n B)/ p(A) .

Lastly, on the third roll, we want the prob of C = 3 given that B has occurred.
P(C|B) = P(C|B|A) = p(A n B|A n C)/p(A n B|A).

And so on and so forth.
You can use a loop to the sequence finding the conditional prob of each till the last.

For N rolls, the prob of the sequence will be
P(N|A|B|C|....|N-1) = P(A n B|A n C|B n...n N-1|N-2 n N)/P(A n B|A n C|B n ...n N-1|N-1) .

But in the real sense of this program as per the question, the outcome of a dice is an INDEPENDENT event.
If two events are independent, the occurrence of one doesn't change the probability of the occurrence of the other. This means that the probability of B occurring, whether A has happened or not, is simply the probability of B occurring.

Therefore, P(A|B) = P(A) == P(N|.....) = P(N) = 1/6.

The independence of the dice makes it simple right?
Last edited on
What the h3ll are you talking about lol...The random function already does all the probability calculations.. You don't need to do anything but call the random function..

@novellof. Will you use the rand() to solve this :
the assignment also says to calculate the chance of that sequence happening and print it as a number from 0-1.
this isn't a programming problem,i don't understand how that works.
can someone give me an example like
what are the chances of the numbers 1,1,3 being rolled.
first off thanks to all the answers.
and @shadowcode,the thing that has me confused is what you said in the last line
the probability is always 1/6th for one dice roll
but i guess its smth like
there is a much higher chance of rolling 3 random numbers
than rolling 3 of the same numbers.so 444 would have a really small chance of happening.
and @hyperfine,i though about this,the chance after each throw of the coin is the same.1 in 2.im confused :S
@noveloff
i thought it would help with making the numbers random(er)
first off im a big noob,so don't laugh.
but since you seed the random operation with the current time,and it gives the same 2 numbers often,i thought it was happening in the exact same time with the exact same seed so thats the reason its giving me the same numbers.so i put a loop,to make some time pass.
and please tell me how stupid and wrong this is,or if there is some logic behind it.
Correct me if I'm wrong, but it sounds like your professor is asking what is the probability that you get a particular sequence of numbers. For example, what is the probability of getting 1526341234 if you roll ten numbers in a row. What I was getting at was that you should think about coin flips instead, and then extend that analysis to dice rolls which are just slightly more complicated. I'm trying not to give you the answer because it's fundamentally important that you understand the statistics of independent events.

Next, to clear up some misconceptions you have about the seed generated by srand. Basically, once you seed the srand() function you have a random string of numbers that you can pull from. It's not like your processor is calling rand() twice before its clock ticks (which wouldn't even make sense). So you don't have to worry about the fact that every time you call rand() you get a result that is completely independent from the last call to rand() (aside from the fact that it's a pseudo RNG). I think Noveloff isn't questioning your usage of the time delay, but he's asking why you have two loops (one in main() and the other in DiceRoll()). Your code only requires one loop.
yes that is correct,that is what the assigment is looking for.
its almost 3 am here,and im really tired,been thinking about this for a while now,but i just can't seem to make the connection.
like i understand that getting 12415 or 12626,or 48683,is more likely than getting 33333,but after that my brain can't think.its also kind of sad since we learned probability in math class like a year ago,but that vanished from my brain(probably because it never was in my brain in the first place,i focused more on things like polynomials at the time).
noveloff now i understand what you meant,and i corrected the codel like you told me.thanks :)
this isn't by any professor by the way,I just wanted to do outside work,because the task they give us in school are too easy for me(not being cocky,they don't require though,they are just like,write a for loop,or smth)
i will be going to sleep now,so i wont reply for some time,but ill read this thread first thing in the morning.
Last edited on
Think about this carefully,
I dont think that the prob of getting 33333 is greater than getting 12415 or 12626,or 48683 or what ever.

Every dice roll is completely independent of the previous.
The prob of getting 3 on the first roll is 1/6.
The prob of getting 3 on the next roll is still 1/6 and is independent of the first 3.
And so on.

e.g
For 2 rolls, the prob of getting the sequence 1,2 is the same as for getting 3,3 and for 6,6 and is equal to 1/36. Coz there are 36 (6^2) different sequences from { 1,1 to 6,6 }.

For N rolls, there are 6^N sequences.
Hence the prob for any of the sequences is 1/(6^N);

The prob of getting 1,1,3 after 3 rolls is 1/(6^3) and is the same for 3,3,3.
ohhh i get it now.
yeah,when i woke up,i though the same about what you said in the first paragraph.
the bigger the number of dice rolled the smaller the chance for one individual sequence to occur is,but its the same for all sequences.
thanks to all of you.
here is how it is in the example btw.

2
3
$ roll-the-dice 3
Rolling 3 dice ...
4 2 8 (Probability: 0.473)
Last edited on
Glad you finally understand all this f**ken probability.
Topic archived. No new replies allowed.