rand() problem, plz help!

This is a discussion on rand() problem, plz help! within the C++ Programming forums, part of the General Programming Boards category; Hey, Im new to these forums and this is my first post, so please dont be too mean =P. Anyways, ...

  1. #1
    Registered User Wylde's Avatar
    Join Date
    Sep 2006
    Posts
    10

    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
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> 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.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> 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.

  4. #4
    Registered User Wylde's Avatar
    Join Date
    Sep 2006
    Posts
    10
    Oooo, I see now, thanks alot =)

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Where is the return 0 in main?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Where is the return 0 in main?
    It's hiding behind the }.
    My best code is written with the delete key.

  7. #7
    Registered User Wylde's Avatar
    Join Date
    Sep 2006
    Posts
    10
    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

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  9. #9
    Registered User Wylde's Avatar
    Join Date
    Sep 2006
    Posts
    10
    Ah ok, thats good to know Thanks

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,487
    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.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  13. #13
    Registered User Wylde's Avatar
    Join Date
    Sep 2006
    Posts
    10
    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.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  15. #15
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 10:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. help plz plz
    By nsssn73 in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21