rand() problem, plz help!

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-17-2006
Wylde
rand() problem, plz help!
Hey, Im new to these forums and this is my first post, so please dont be too mean =P. Anyways, Im tryin to Learn C++, and im messin around with rand() and functions just so I can get a feel for how they work. So, I wrote this program that takes in 2 numbers, the first is how many 6-sided dice you want it to roll, and the second is how many times you would like to simulate the rolling. It then counts the number of times that 1,2,3,4,5,6 comes up and totals them up. It works pretty good except for 1 thing, i never get any sixes, I looked at the code for a while and can't figure out why its doing that. So if someone could please take the time to find out where I went wrong I would really appreciate it =). THanks alot!

Code:

```#include <iostream> #include <math.h> #include <time.h> #include <stdlib.h> using namespace std; void random(int n); int num_of_dice=1,num_of_times=1; int main() {         srand(time(NULL));         while(num_of_dice)         {         cout << "How many dice should we roll?";         cin >> num_of_dice;         cout << endl;         cout << "How many time should we roll " << num_of_dice << " dice?";         cin >> num_of_times;         cout << endl;         random(num_of_dice);         } } void random(int n) {         int k1=0,k2=0,k3=0,k4=0,k5=0,k6=0;         int kT=0; for(int q=1;q<=num_of_times;q++) {         for(int i=1;i<=num_of_dice;i++)         {         int r=(rand()%5)+1;         switch(r)                 {         case 1:                                 k1++;                                 break;         case 2:                                 k2++;                                 break;         case 3:                                 k3++;                                 break;         case 4:                                 k4++;                                 break;         case 5:                                 k5++;                                 break;         case 6:                                 k6++;                                 break;         default:                                 cout << "Error" << endl;                                 break;                 }         }         kT=k1+k2+k3+k4+k5+k6;         cout << "One : " << k1 << endl << "Two : " << k2 << endl << "Three : ";         cout << k3 << endl << "Four : " << k4 << endl << "Five : " << k5 << endl;         cout << "Six : " << k6 << endl << "Total : " << kT << endl << endl; } }```
I attached the cpp file too. I think :confused:
• 09-17-2006
Daved
>> int r=(rand()%5)+1;
rand()%5 will give numbers from 0 to 4 (including 0 and 4). Add one to that and you get 1-5.
• 09-17-2006
twomers
>> r=(rand()%5)+1;

returns between less than 6.

r=(rand()%6)+1;

Also,

>> int num_of_dice=1,num_of_times=1;

They don't really need to be global.
• 09-17-2006
Wylde
Oooo, I see now, thanks alot =)
• 09-17-2006
swgh
Where is the return 0 in main?
• 09-17-2006
Prelude
>Where is the return 0 in main?
It's hiding behind the }.
• 09-17-2006
Wylde
Quote:

Originally Posted by swgh
Where is the return 0 in main?

Guess I forgot that too :( it still ran fine, and I got no errors. weird :confused:
• 09-17-2006
Prelude
>it still ran fine, and I got no errors. weird
It's not weird at all. You didn't get any errors because it's not a constraint violation. If you omit the return statement, C++ returns 0 automagically.
• 09-17-2006
Wylde
Ah ok, thats good to know :D Thanks
• 09-17-2006
Mario F.
It's not needed. The standard defines it as optional to place the return statement in main().

Personally, I can't seem to figure out why on earth they would decided that. Never agreed to that . So in my futile attempt to look smarter than the folks at the standards committee, I always place return 0 nonetheless. :)
• 09-17-2006
swgh
MarioF - good argument but I think is all down to how the individual is taught. Be it in college, school or through books. Most if not all the books I have read on C++ says it is recomended but not essential to insert return 0 before the closing brace in the main function.

At the end of the day it is down to the programmer's discretion, not the standard
• 09-17-2006
Prelude
>Personally, I can't seem to figure out why on earth they would decided that.
You're not the only one. The best argument is that it keeps the void mainers from claiming that their way requires fewer keystrokes due to the lack of a return statement. Of course, that's hardly a reason to standardize an exception.
• 09-17-2006
Wylde
Quote:

Originally Posted by twomers
>> int num_of_dice=1,num_of_times=1;

They don't really need to be global.

Ya, I guess they dont, but I dont really understand why it matters. I mean, I think I understand the difference between local and global variables for the most part, but I dont see why you cant just make all your variables global and use different names for them to keep them from interfering with eachother.
• 09-17-2006
Prelude
>but I dont see why you cant just make all your variables global and use different names for
>them to keep them from interfering with eachother.
You can certainly do that. Oh, and then add four or five third party libraries. And add a few dozen features and the corresponding 50,000 lines of code. Then try to catch a bug where you inadvertently change one of them. That's why we say to avoid globals. :)
• 09-17-2006
swgh
Let me just say that using global variables in C++ is a big fat no no!

Global variables make it very hard to keep track of when it is changing its value and where it changes, say you had a file of 2000 lines of code. And you wanted to keep track of a certain variable at a certain point, ie: when did it chnage its value from a=1 to a=5? It would take ages to find out.

Local variables are always defined inside a functon, and are lost or destroyed when the function ends or returns. The only two exceptions to this is if you pass the variables to a function, then the function gets a "copy" of the values, which stay the same. Also, it is usually better to pass by reference than pass by value. The other exceoption os if you made the local variablex static, ( simular to global but not totally ), they retain there value between a functiohn call. Global variables stay in memory untill the program terminates too, making up lots of space where as local do not take up as much
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last