RNG in C++

This is a discussion on RNG in C++ within the C++ Programming forums, part of the General Programming Boards category; Well, the following is an exercise that I have to use RNG with it, but I don't have any experience ...

  1. #1
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49

    Question RNG in C++

    Well, the following is an exercise that I have to use RNG with it, but I don't have any experience related to such function (RNG), so I can't understand how to relate between the program and the code at the end of the post which generates random numbers, I am kind of confused between the N & M used in the exercise, and those M & n used in the note.
    I can not understand exactly the code at the end of the post.
    Thank you,

    -Amy


    Exercise:
    -------
    Use a random number generator to simulate N throws of a pair of dice and to give the number of times their sums were 7 or 11. Allow the program to input N (e.g. N = 10, 100, 1000) and to repeat the simulation M runs for the same N (e.g. M = 10). For each N, find the average probability of 7 or 11 for the M runs (sum the results for the M runs and divide by M then divide by N).

    Note on using the C++ Random Number Generator (RNG):
    -----------------------------------------------------
    This program uses random numbers generated by a Random Number Generator (RNG). The RNG in C++ is a function rand( ) that returns a random integer from 0 to 32,767. To obtain random integers from 1 through n, use rand( ) % n + 1. To obtain a random sequence you need to first initialise the RNG using the time of the machine as a seed. This is done so that we do not get the same sequence every time we run the program.
    The following is an example of how to generate a random sequence of M integers with values (x) between 1 and n :

    Code:
    #include <time.h>
    int i , x , n , M
    srand ( (unsigned) time (NULL) );        //Initialize RNG
    for ( i = 1; i <= M; i++)              // Loop over the sequence
    {            
        x = rand( ) % n + 1;            // Generate a number from the sequence
        cout << x << endl;            // Print it
    }
    It ain't illegal until you get caught..

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well in that code, a random number between 1 and n is generated M times...

    in the description, it looks like they want you to throw the dice N times. let's use 10 for example. then it says that it wants you to thow the dice that many times for M times. let's use 5 there. so, now you have N as 10 and M as 5. what it's saying is to create a program to simulate N(10) dice throws M(5) times, and find the probability that the sum will either be 7 or 11 on any given throw.

    basically, keep the M and N seperate from the M,n,i, and x from the program code. that was probably not a very good choice on your instructors part. the question has some difficult wording as well, but that's something you'll need to get used to in programming classes...

    FYI: it's not really a RNG, it's actually more of a PRNG, or a Psuedo Random Number Generator, because the numbers it generates are not truly random, but it'll be good enough for now...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    Quote Originally Posted by major_small
    well in that code, a random number between 1 and n is generated M times...

    in the description, it looks like they want you to throw the dice N times. let's use 10 for example. then it says that it wants you to thow the dice that many times for M times. let's use 5 there. so, now you have N as 10 and M as 5. what it's saying is to create a program to simulate N(10) dice throws M(5) times, and find the probability that the sum will either be 7 or 11 on any given throw.

    basically, keep the M and N seperate from the M,n,i, and x from the program code. that was probably not a very good choice on your instructors part. the question has some difficult wording as well, but that's something you'll need to get used to in programming classes...

    FYI: it's not really a RNG, it's actually more of a PRNG, or a Psuedo Random Number Generator, because the numbers it generates are not truly random, but it'll be good enough for now...
    Aha, well, now I will try working on the program and post it once I am done with it to see if it is made the right way, thanks for clarifying.

    - Amy
    It ain't illegal until you get caught..

  4. #4
    Amy amirahasanen1's Avatar
    Join Date
    Jul 2003
    Posts
    49
    So I think I am done with the program and it's working fine, that's the code, if there are any comments please add them here.
    Thank you,

    -Amy


    Code:
    #include <iostream>
    #include <time.h>
    
    using namespace std;
    
    int main()
    {
    	int iN, iM, x1, x2, n , N, M;
    	float counter = 0;
    	int sum;
    
    	n = 6;
    	N = 10;
    	M = 5;
    
    	for ( iM = 1; iM <= M; iM++ )
    	{
    		
    		srand ( (unsigned) time (NULL) );		//Initialize RNG
    		for ( iN = 1; iN <= N; iN++ ) // Loop over the sequence
    		{
    			x1 = rand( ) % n + 1;	// Generate a number from the sequence
    			cout<<x1<<" ";			// Print it
    			
    			x2 = rand( ) % n + 1;	// Generate a number from the sequence
    			cout<<x2<<endl;			// Print it
    
    			sum = x1 + x2;
    
    			if((sum==7) || (sum==11))
    				counter += 1;		
    		}
    
    		cout<<endl;
    	}
    
    	cout<<"Average Probability: "<<(counter/M)/N;
    
    	cin.get();
    	return 0;
    }
    It ain't illegal until you get caught..

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    looks good to me. I would only make a very few, very slight changes to the code you had:
    Code:
    #include <iostream>
    #include <ctime>        //slightly more standard
    
    using namespace std;
    
    int main()
    {
        int iN, iM, x1, x2, n , N, M;
        float counter = 0;
        int sum;
    
        n = 6;
        N = 10;
        M = 5;
    
        for ( iM = 1; iM <= M; iM++ )
        {
    
            srand ( (unsigned) time (0) );        //I usually try to avoid macros
            for ( iN = 1; iN <= N; iN++ )
            {
                x1 = rand( ) % n + 1;    
                cout<<rand( ) % n + 1<<" ";          
    
                x2 = rand( ) % n + 1;  
                cout<<x2<<endl;         
    
                sum = x1 + x2;
    
                if((sum==7) || (sum==11))
                    counter++;  //style
            }
    
            cout<<endl;
        }
    
        cout<<"Average Probability: "<<(((counter/M)/N)*100)<<'%';
        //using a percentage instead of a decimal
    
        cin.ignore(1); //style over substance
        return 0;
    }
    like I said in that last comment, it's all style over substance, and these changes are opinion rather than fact.

    good coding.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,394
    I think you could place the srand() call before the first for loop - you shouldnt need to call it multiple times.

    Also, using rand() % n can lead to a slight bias if the range of possible random integers is not congruent to n (i.e. RAND_MAX+1 is not 0 (mod n) )
    There are ways to fix this, and you might want to explore them for fun - or for marks if your teacher considers your solution as creative or something.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ternary and commas
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 01-04-2009, 10:28 PM
  2. problem with boost random func (again)
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2006, 02:22 AM
  3. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 10:05 AM

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