I'm supposed to write a program that rolls two dice and adds the sum together. I'm supposed to use the rand() fucntion and something called srand(time(0)) for this program. The problem is that I've never used this type of function before and they never bothered explaining it to me in class. Could someone explain how to use this function and what rand() and srand(time(0)) means?
25 Replies - 4770 Views - Last Post: 30 October 2008 - 06:24 AM
Replies To: Rolling Dice
#2
Re: Rolling Dice
Posted 29 October 2008 - 05:46 PM
rand() returns a random number.
srand(time(0) ) seeds the random number generator.
rand reads a list of numbers from somewhere (or so I have been told) and unless you seed the generator with a number you will get the same sequence every time.
the time function returns the number of seconds since Jan 1st, 1970. since the time function returns a new value every second it is the ideal way to seed the random number generator.
srand(time(0) ) seeds the random number generator.
rand reads a list of numbers from somewhere (or so I have been told) and unless you seed the generator with a number you will get the same sequence every time.
the time function returns the number of seconds since Jan 1st, 1970. since the time function returns a new value every second it is the ideal way to seed the random number generator.
#3
Re: Rolling Dice
Posted 29 October 2008 - 06:05 PM
GWatt, on 29 Oct, 2008 - 05:46 PM, said:
rand() returns a random number.
srand(time(0) ) seeds the random number generator.
rand reads a list of numbers from somewhere (or so I have been told) and unless you seed the generator with a number you will get the same sequence every time.
the time function returns the number of seconds since Jan 1st, 1970. since the time function returns a new value every second it is the ideal way to seed the random number generator.
srand(time(0) ) seeds the random number generator.
rand reads a list of numbers from somewhere (or so I have been told) and unless you seed the generator with a number you will get the same sequence every time.
the time function returns the number of seconds since Jan 1st, 1970. since the time function returns a new value every second it is the ideal way to seed the random number generator.
The problem asks to roll the dice 10000 times. So would I write srand(time(10000))? And where would I place that in the program?
As for rand() do I put a number inside of there to indicate the possibilites? And if I wanted to add the results of two rand functions would I just write:
rand1() + rand2() = sum
Sorry for all of the question but I want to make sure I know what I'm doing before I end up with a ton of mistakes.
#4
Re: Rolling Dice
Posted 29 October 2008 - 06:13 PM
actually, you only need to call srand once.
if you want to roll a dice 10,000 times then use a loop
while(count++ < 10000)
and rand takes 0 arguments
if you want to roll a dice 10,000 times then use a loop
while(count++ < 10000)
and rand takes 0 arguments
#5
Re: Rolling Dice
Posted 29 October 2008 - 06:18 PM
So how would I make it so rand() will only produce a number between 1 and 6.
#6
Re: Rolling Dice
Posted 29 October 2008 - 06:30 PM
(rand() % 6) + 1
the % returns the remainder of the division of the value on the left by the value on the right.
e.g. 7893 / 6 is 1315 R 3 so 7893 % 6 would be 3.
the % returns the remainder of the division of the value on the left by the value on the right.
e.g. 7893 / 6 is 1315 R 3 so 7893 % 6 would be 3.
#7
Re: Rolling Dice
Posted 29 October 2008 - 06:55 PM
So I'm writing the program now and I came across my first problem. I'm supposed to count the number of times a number comes up by using an array and that's where I get stuck.
Here's my program so far:
Here's my program so far:
#include <iostream>
#include <cstdlib>
Srand(time(0));
using namespace std;
int main()
{
int rand(), result, count=0, sum[10]={2,3,4,5,6,7,8,9,10,11,12}, rollsum=0, counter[10];
while (count++ < 10000)
{
for (int i = 0; i < 2; i++)
{
result = radn(()%6)+1;
rollsum = rollsum + result;
}
counter[sum[]]
}
return 0;
}
#8
Re: Rolling Dice
Posted 29 October 2008 - 07:08 PM
you don't need to declare the random function, and you should probably put the srand call into your main function.
are you supposed to roll two dice at once and calculate the total number of times their sum occurs (possible values 2-12), or are you supposed to find how many times each individual number occurs (values 1-6)?
are you supposed to roll two dice at once and calculate the total number of times their sum occurs (possible values 2-12), or are you supposed to find how many times each individual number occurs (values 1-6)?
#9
Re: Rolling Dice
Posted 29 October 2008 - 07:10 PM
GWatt, on 29 Oct, 2008 - 07:08 PM, said:
you don't need to declare the random function, and you should probably put the srand call into your main function.
are you supposed to roll two dice at once and calculate the total number of times their sum occurs (possible values 2-12), or are you supposed to find how many times each individual number occurs (values 1-6)?
are you supposed to roll two dice at once and calculate the total number of times their sum occurs (possible values 2-12), or are you supposed to find how many times each individual number occurs (values 1-6)?
I need to count how many times their sums appear.
#10
Re: Rolling Dice
Posted 29 October 2008 - 07:19 PM
I found out some more information about the counter array I'm supposed to use.
The problem states:
Declare an array "counter" that holds the number of times each sum occured. The array will hold 11 integer elements. Each element is initally assigned to 0 and as the program proceeds (loop goes throught the 10000 loops), each element is incremented by one.
The problem states:
Declare an array "counter" that holds the number of times each sum occured. The array will hold 11 integer elements. Each element is initally assigned to 0 and as the program proceeds (loop goes throught the 10000 loops), each element is incremented by one.
#11
Re: Rolling Dice
Posted 29 October 2008 - 07:21 PM
Then I would get the a random number twice in each loop like so
while(count++ < 5000)
{
int diceOne = rand() % 6 + 1;
int diceTwo = rand() % 6 + 1;
sum += diceOne + diceTwo;
counter[diceOne + diceTwo]++;
}
#12
Re: Rolling Dice
Posted 29 October 2008 - 07:27 PM
Would this work?
The observed and theoretical are other parts of the program. The theoretical values are given and the observed is calculated by taking the number of times a sum was rolled divided by the number of times rolled.
#include <iostream>
#include <cstdlib>
Srand(time(0));
using namespace std;
int main()
{
int result, count=1, sum[10]={2,3,4,5,6,7,8,9,10,11,12}, rollsum=0, counter[10]={0,0,0,0,0,0,0,0,0,0,0};
double theoretical[10]={2.778,5.556,8.333,11.111,13.889,16.667,13.889,11.111,8.333,5.556,2.779), observed;
while (count++ < 10001)
{
for (int i = 0; i < 2; i++)
{
result = radn(()%6)+1;
rollsum = rollsum + result;
}
counter[rollsum-2]=counter[rollsum-2]+1;
observed = (counter[rollsum-2]/count)*100;
cout<<result<<"\t"<<theoretical[rollsum-2]<<"\t"<<observed;
}
return 0;
}
The observed and theoretical are other parts of the program. The theoretical values are given and the observed is calculated by taking the number of times a sum was rolled divided by the number of times rolled.
This post has been edited by duskmon10: 29 October 2008 - 07:29 PM
#13
Re: Rolling Dice
Posted 29 October 2008 - 07:29 PM
I suppose, but that means you actually roll the dice 20,000 times.
#15
Re: Rolling Dice
Posted 29 October 2008 - 07:38 PM
duskmon10, on 29 Oct, 2008 - 07:30 PM, said:
Why does it roll 20000 times?
Ignore my last question. I went ahead and rewrote my program:
#include <iostream>
#include <cstdlib>
Srand(time(0))
using namespace std;
int main()
{
int result, count=1, sum[10]={2,3,4,5,6,7,8,9,10,11,12}, rollsum=0, counter[10]={0,0,0,0,0,0,0,0,0,0,0};
double theoretical[10]={2.778,5.556,8.333,11.111,13.889,16.667,13.889,11.111,8.333,5.556,2.779}, observed;
cout<<"Sum "<<"\t"<<"Theoretical Results"<<"\t"<<"Observed Results"<<endl;
while(count++ < 5000)
{
int diceOne = rand() % 6 + 1;
int diceTwo = rand() % 6 + 1;
result= diceOne + diceTwo;
counter[result-2]++;
observed = (counter[result-2]/count)*100;
cout<<result<<"\t"<<theoretical[rollsum-2]<<"\t"<<observed;
}
return 0;
}
But I'm still geting some errors in my program.

New Topic/Question
Reply



MultiQuote


|