+ 5
Random Number Die Roll
I need help writing a program in C++ that will roll a die and count how many times the die has been rolled before rolling 1,2,3,4,5 & 6 in order.
27 Answers
+ 8
You'll need to use 2 libraries to generate random numbers.
1. ctime
2. cstdlib
Use srand and rand functions
srand seeds value for pseudo-random generator
rand returns random value based on seeded value. using rand without any call to srand will give results as if srand was seeded by 1. It'll not give you really random nos so pass time(0) as arg to srand as srand(time(0));
Now rand is ready to use but every time it'll return value b/w 0 and MAX_RAND so trick is using %.
Using rand()%n will give random no b/w 0 to n.
To get no.s b/w 1 to 6 use 1+rand()%6;
Why % works? Well, modulus division by n makes sure that result will be less than n.//got it?
end note:
Always post your attempt when you ask for help about any code.
+ 7
Varsha Can you please write this code on Sololearn Playground. So we can do debug and check the problem?
+ 5
Have you try to do by yourself. If yes then what problem you are facing. You can share with your code here so we can help.
First try to do by yourself to learn coding and logic build-up.
+ 4
Habib Khan Please don't ask your doubt in others thread.
You can ask seperately.
+ 3
Phinda This is not right thread to ask programming. Ask in seperate thread but doubt not any program. Because everyone here are learning so no-one is sitting blank to write your code.
+ 2
x = 1 + rand() % 6;
The above will output a pseudo-random number between 1 and 6 (a dice roll) to the x variable every time it's called.
You can use a for-loop and an if statement to check whether the first roll is a 1, the second is a 2, etc, re-calling the loop recursively whenever the rolls are wrong. Remember to increment a totalRolls variable every time you roll, and you should be set.
I hope that makes it clear how to do this, without giving away all of the fun of figuring it out :P
(Disclaimer: I'm brand new to this, and very open to C&C of my suggestion)
+ 2
Vraj Patel Don't spam in any thread. Your account may get blocked. So be careful. And also don't do anything like posting mobile number or other things. Be a good man.
+ 1
Yes I tried doing it myself and I was getting stuck on the part where I'm suppose to roll 1,2,3,4,5,6 in order.
+ 1
Varsha This is your code written by you and it's working fine.
https://code.sololearn.com/cOwJW4YikuR4/?ref=app
+ 1
Rekulapally Suma I love you too. Beta aise sabke samne didi ko propose nhi karte.
You are here to learn something so don't do this type of things here.
0
Additionally, I would also recommend to search the Code Playground for examples first. You can see how others are doing it, learn from them, and make adjustments/improvements where you find necessary.
Search by 'dice roll' keywords 👍
0
I'm fairly new to programming so please excuse my terrible coding.
0
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
//Get seed value
unsigned seed = time(0);
//Variables
srand(seed);
int count;
int one;
int two;
int three;
int four;
int five;
int six;
//Generate random numbers
for (count = 1; count >= 1; count++)
{
one = rand() % 6 + 1;
two = rand() % 6 + 1;
three = rand() % 6 + 1;
four = rand() % 6 + 1;
five = rand() % 6 + 1;
six = rand() % 6 + 1;
if (one == 1 && two == 2 && three == 3 && four == 4 && five == 5 && six == 6)
{
cout << one << "\n";
cout << two << "\n";
cout << three << "\n";
cout << four << "\n";
cout << five << "\n";
cout << six << "\n";
break;
}
}
cout << endl;
cout << "It took " << count << " rolls to roll 1, 2, 3, 4, 5 & 6." << endl;
}
0
I hope this works^
0
1.you don't need to make "seed" variable and then pass it to srand it's better to do it like: srand(time(0))
2.start count from 0, at the beginning you haven't found anything
3.U r rolling a dice six times and then check if you've got 123456
and if not roll six times again but what about sequences like this
111123,456321 which there is 123456 in it but ur code doesn't check it
just think about code's logic flow first(use a paper it helps)
U r close,u can do it
0
Write a program using c++ which will show your full name, id number,class your are in number of days attended and percentage bonus. Bonus is 0.5
0
If you want to make a counter in a loop that will cycle an unknown amount of times you could do:
for(int count = 1;; ++count)
What you're doing there is a for loop without exit condition, that will loop infinite times, but will also have a counter that you could use in every iteration.
0
this will do
https://code.sololearn.com/c5P1LVeZvvEX/?ref=app
tried to make it easy
if there is anything wrong about the code sorry. tell me and I'll fix it.
0
I need help for my first project in universty i have 5 days to select tittle plz help to select tittle. And give me a unique ideas for unique project. I will be very thankful to you guys