| | |
Help with rand please.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Solved Threads: 0
Please help me im having trouble with a dice roller im making.
Its kind of working except i was wondering why it is that every time i start it up it always throws up the same numbers in a row if you put in 100. It probably does that with other numbers to but i haven't checked them.
Anyhow i was wondering if someone could suggest a way to make it actually random, if its possible in C++ and C.
Any suggestions in just imporving it are welcome to
THANKS ALL.
Its kind of working except i was wondering why it is that every time i start it up it always throws up the same numbers in a row if you put in 100. It probably does that with other numbers to but i haven't checked them.
Anyhow i was wondering if someone could suggest a way to make it actually random, if its possible in C++ and C.
Any suggestions in just imporving it are welcome to
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <string> using namespace std; void loop_q(); int main() { int dice; system("cls"); cout << "What would you like your dice out of: " << endl; cout << "(100, 10, 6, 4 ect..)" << endl; cin >> dice; int rand_number = rand() % dice + 1; system("PAUSE"); system("cls"); cout << "Roll: " << rand_number << endl; cout << endl; loop_q(); } void loop_q() { cout << "Would you like to roll again? y/n." << endl; char answer; cin >> answer; if (answer=='y') { main(); } else { return; } }
It generates the same set of numbers every time because you didn't seed it. Do this:
C++ Syntax (Toggle Plain Text)
#include <ctime> int main() { srand( time(0) ); // see the random number generator // other code here }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
>It works good now, but i would like to know how it works.
These two links should give you more information than you probably want right now.
http://www.eternallyconfuzzled.com/t..._tut_rand.aspx
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
These two links should give you more information than you probably want right now.

http://www.eternallyconfuzzled.com/t..._tut_rand.aspx
http://www.eternallyconfuzzled.com/a..._art_rand.aspx
I'm here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: gradually hidding an output
- Next Thread: help with pseudo program
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






