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 :icon_cheesygrin: THANKS ALL.

#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;
}

}
Ancient Dragon commented: Thanks for using code tags +19

It generates the same set of numbers every time because you didn't seed it. Do this:

#include <ctime>

int main()
{
     srand( time(0) ); // see the random number generator

    // other code here
}

Thanks very much!!

i just have one more question as i wish to learn and am teaching myself from the internet.

It works good now, but i would like to know how it works. If you would care to explain it to me that would be great.

Thanks again mate.:icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.