# Thread: chance assignment of random

1. ## 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. Integers are integers, so you can't store decimal numbers in them (like 0.44).
Use a range from 0-100 instead.

3. ## ?

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

4. 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>

5. 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