chance assignment of random

This is a discussion on chance assignment of random within the C++ Programming forums, part of the General Programming Boards category; Hi, I need to be pointed towards the correct way to express, a greater percent chance of something happending. For ...

  1. #1
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    chance assignment of random

    Hi, I need to be pointed towards the correct way to express,
    a greater percent chance of something happending.
    For example:

    a and b are playing cards and a has a 60% chance of winning.
    and the wins are generated by rand.(got that part) it's just
    the percentages
    Thanks
    Michele
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    #include<iomanip>
    #include<iostream>
    #include<fstream> 
    using namespace std;
    
    
    //function prototype
    int pp (int&);
    int score = 0;
    
    void main()
    {
    
    ofstream outfile("A:\\0F1017_6_2.txt"); 
    if(!outfile){ 
    cerr << "Cannot open output file" << endl; 
    
    } 
    
    srand((unsigned) time(0));
    int a = 0(.56);
    int b = 0(.44);
    int aa = 0;
    int bb = 0; 
    ............................
    //my function is:
    	int pp (int &score)           	//function pp
    			{score = ( rand() % 2); 
    			return score;}
    i thought that by assigning them to the initialization of the int of each it would work, but i cannot tell if it is...is it?
    should i be expressing the chances in the random expression of the function?
    thanks Michele/scuba22

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Integers are integers, so you can't store decimal numbers in them (like 0.44).
    Use a range from 0-100 instead.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35

    ?

    so
    something like:
    Code:
    int a = 0(1-56);
    int b = 0(57-100);
    ???

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I have no idea what 0(12345) does.

    This calculates a random percentage in the range 1-100, and checks if Player1 or Player2 wins.
    Code:
    int GetPercent()
    {
       return ((rand() % 100) + 1);
    }
    
    int main()
    {
       srand(time(NULL));
       int Result = GetPercent();
    
       if(Result <= 56)
       {
          cout << "Player 1 wins!" << endl;
       }
       else
       {
          cout << "Player 2 wins!" << endl;
       }
    
       return 0;
    }
    PS: Notice the int main()

    <edit>
    Are you aware that you include unneccessary headers, and one is included twice?
    </edit>
    Last edited by Magos; 10-23-2002 at 10:27 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User scuba22's Avatar
    Join Date
    Oct 2002
    Posts
    35
    hi, i didn't realize iostream was in there twice...i'm not sure what isn't necessary though?

    #include<iostream> is this the unecessary?
    #include<cstdlib> needed for rand fx
    #include<ctime> for my time seed
    #include<iomanip> for my setw()
    #include<fstream> and for my outfile.....(that's my ultimate goal to print to a file)
    ____________________________
    i don't understand your explanation...
    what i need to do is write a program for 3 "games" to be won at a score of 11.
    Player A has a 56% chance of winning, player B has a 44% chance.
    here is the entire thing..maybe you can point to how i can encorporate your method into my loops or function.
    Sorry if I am dense, I am 6 weeks into C++ :-D.
    Thx
    Michele
    Code:
    //Michele DAddio CS210
    //10_17_02::HW #6 : RANDOM
    
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    #include<iomanip>
    #include<fstream> 
    using namespace std;
    
    
    		//function prototype
    			int pp (int&);
    			
    
    		int main()
    		{
    
    			ofstream outfile("A:\\0F1017_6_2.txt"); 
    			if(!outfile){ 
    				cerr << "Cannot open output file" << endl; 
    			  
    		} 
    	
    			srand((unsigned) time(0));
    			int a = 0(1-56);
    			int b = 0(57-100);
    			int aa = 0;
    			int bb = 0;
    			int score = 0;
    		
    		for (int pg=0; pg < 3;pg++){
    
    			outfile << "\t\t\n Game " << pg +1 << endl;
    
    		bool zz = true;	
    		if (zz == true){
    		outfile << setw(11) << "\n\tAda" << setw(16) << "Blaise" << endl;
    		bool zz = false;}
    
    
    		for ( int c = 0; c <= 22; c++) {
    			
    			score = pp(score);
    			if (score == 1){
    				outfile << setw (20) << a+1 <<setw(15)<< "0" << endl;
    				a++;
    				aa++;}
    			else {
    				outfile << setw (20)<< "0"<< setw(15) << b+1 << endl;
    				b++;
    				bb++;}
    
    
    			if (a == 11){
    				outfile << setw(10) << "\tAda won 11 to " << b << endl;
    			a = 0;
    			b = 0;
    			break;}
    			else if (b == 11){
    				outfile << setw(10) << "\tBlaise won 11 to " << a << endl;
    			a = 0;
    			b = 0;
    			break;}
    				
    			}
    		}
    				
    			outfile <<"\t\t\n";
    			if ( aa > bb ){
    				outfile << "Ada is the overall winner\n";}
    			else {outfile << "Blaise is the overall winner\n";}
    
    			return 0;
    
    	}
    
    			int pp (int &score)           	//function pp
    			{score = ( rand() % 2); 
    			return score;}
    &#91;code]&#91;/code]tagged by Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 05:00 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 04:16 PM
  4. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 04:23 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM

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