Anyone knows what are percents...

Some event has some chances to happen. From 0 to 100... if the percent is 0 it means zero chances to happen. 100 means it will happen every time. 50 statistically means it will happen half of times...

But HOW can someone give x/100 chances for an event to happen in C++?
This is a question, now an answer.

I was thinking to generate a list of X (=the percent) numbers and see how many of them... well... this is where i stop. How many of them are divisible with 2, or 3, or...or, well, why should i use a certain number?

MAYBE THERE IS A EASY WAY. If there is a function that returns "1" or "0" after you give it the percent, please tell me! And can i make the function more precise? I mean, maybe it will work on 1/10,000 chances, not on 1/100.

Something like this?

``````#include <iostream>
#include <cstdlib>
#include <ctime>

int foo(double percent)
{
double number = std::rand() / (double)RAND_MAX; /* value from [0,1] */
return number < percent;
}

int main()
{
std::srand(std::time(0));
double pct = 0.1;
for ( int i = 0; i < 10; ++i )
{
std::cout << "foo(" << pct << ") = " << foo(pct) << std::endl;
}
return 0;
}

/* my output
foo(0.1) = 0
foo(0.1) = 0
foo(0.1) = 1
foo(0.1) = 0
foo(0.1) = 0
foo(0.1) = 0
foo(0.1) = 0
foo(0.1) = 0
foo(0.1) = 0
foo(0.1) = 0
*/``````

I am starting to get used to your replies, "Dave Sinkula". I know now that every time you reply, my problem is already solved.
Ok, so look what i have in mind, related to the percent thing:

``````#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int Z_rand;
double if_perf_div;
int Percents(int ZzZz_percents)
{
Z_rand=rand()%ZzZz_percents;
cout<<"Z_rand = "<<Z_rand<<"   ";
if_perf_div=Z_rand%(100/Z_rand);
//- Used for 100%=every time and 0%=never. -//
if (if_perf_div == 0) cout <<"-:::Event happened:::-"<<endl;
else cout <<"-:::Sorry, it didn't happen:::-"<<endl;
return 0;
}
int main()
{
srand(time(0));
cout<<"The ZzZz_percent to happen is: 20!"<<endl;
int to_100 = 20;     // ZzZz_percents is here!
for (int i=0; i<=50; ++i)
{
Percents(to_100);
}
return 0;
}``````

I managed to compile it in BC++ and Dev-C++, but i get an error every time i run the program, after a few lines... What seems to be the problem? Your program didn't have any errors.

``````Z_rand=rand()%ZzZz_percents;
...........................................
if_perf_div=Z_rand%([B]100/Z_rand[/B]);``````

What if Z_rand == 0 ?

How can i make it start from 1 than? I NEED THE PROGRAM!

Hmmm, i now have problems with Dave Sinkula's code too... I am using it to generate chances for HitRoll...

Perhaps I'm a little slow today, but what is your expected output?

Ok.

int my_dex, his_dex;
double rap_junk1, rap_junk2;
rap_junk1=(his_dex + his_dex*1/2)/(my_dex + my_dex*3/10);
//HisToHit / MyToDodge; After the resunt, i know if i dodge.
rap_junk2=( my_dex + 1/2*my_dex )/( his_dex + 3/10*his_dex );
//MyToHit / HisToDodge; After the result i know if he dodges.

Now i want to use your function to do exactly what it does... If it returns 0, the dodge doesn't happen, so it is a hit.
Theoretically it should work, i know.

If this function doesn't work, maybe i can repair My Function. Its very nasty to use my func, yours its perfect cose it uses 0-1 percents...

Hmm. You want to feed the function an integral percentage [0,100]?

``````#include <iostream>
#include <cstdlib>
#include <ctime>

int foo(int pct)
{
double percent = pct / 100.0;
double number = std::rand() / (double)RAND_MAX; /* value from [0,1] */
std::cout << "number = " << number << ", ";
return number < percent;
}

int main()
{
std::srand(std::time(0));
for ( int pct = 10; pct < 100; pct += 40 )
{
std::cout << "pct = " << pct << std::endl;
for ( int i = 0; i < 10; ++i )
{
std::cout << "foo(" << pct << ") = " << foo(pct) << std::endl;
}
}
return 0;
}

/* my output
pct = 10
number = 0.42906, foo(10) = 0
number = 0.769005, foo(10) = 0
number = 0.739586, foo(10) = 0
number = 0.872433, foo(10) = 0
number = 0.668142, foo(10) = 0
number = 0.0581378, foo(10) = 1
number = 0.89349, foo(10) = 0
number = 0.234077, foo(10) = 0
number = 0.175909, foo(10) = 0
number = 0.583728, foo(10) = 0
pct = 50
number = 0.515244, foo(50) = 0
number = 0.87582, foo(50) = 0
number = 0.343089, foo(50) = 1
number = 0.584155, foo(50) = 0
number = 0.841426, foo(50) = 0
number = 0.795251, foo(50) = 0
number = 0.808374, foo(50) = 0
number = 0.375225, foo(50) = 1
number = 0.683157, foo(50) = 0
number = 0.908231, foo(50) = 0
pct = 90
number = 0.51442, foo(90) = 1
number = 0.131993, foo(90) = 1
number = 0.940275, foo(90) = 0
number = 0.605365, foo(90) = 1
number = 0.765923, foo(90) = 1
number = 0.774621, foo(90) = 1
number = 0.601459, foo(90) = 1
number = 0.0839564, foo(90) = 1
number = 0.383312, foo(90) = 1
number = 0.925932, foo(90) = 0
*/``````

I have a big problem:

``````double rap_junk1= his_hr / my_2d;
double rap_junk2= my_hr / his_2d;

unsigned short my_dex=100;
//Its imported from a file and couted on the
//screen. I checked and the value is 100.
my_hr = my_dex*3/2;
my_2d = (my_dex*13/10 + 1);

unsigned short his_dex=90;
//Imported and checked. Its 90.
his_hr = his_dex*3/2;
his_2d = (his_dex*13/10 + 1);``````

<< moderator edit: added [code][/code] tags >>

Now, i dont have any problems when i declare "rap_junk1" and "rap_junk2" and give no value for them. They are zero and everything works quite well...
But when i declare them are the raport between his_hr and my_2d, i compile it and when i run it i get an error!
WHY DO I GET AN ERROR FOR A DIVISION?

I really dont understand...

>I really dont understand...

Maybe I don't either.

``````#include <iostream>

int main()
{
unsigned short my_dex=100;
unsigned short his_dex=90;
std::cout << "my_dex = " << my_dex << std::endl;
std::cout << "his_dex = " << his_dex << std::endl;

int my_hr = my_dex*3/2;
int my_2d = (my_dex*13/10 + 1);
std::cout << "my_hr = " << my_hr << std::endl;
std::cout << "my_2d = " << my_2d << std::endl;

int his_hr = his_dex*3/2;
int his_2d = (his_dex*13/10 + 1);
std::cout << "his_hr = " << his_hr << std::endl;
std::cout << "his_2d = " << his_2d << std::endl;

double rap_junk1= his_hr / my_2d;
double rap_junk2= my_hr / his_2d;
std::cout << "rap_junk1 = " << rap_junk1 << std::endl;
std::cout << "rap_junk2 = " << rap_junk2 << std::endl;

return 0;
}

/* my output
my_dex = 100
his_dex = 90
my_hr = 150
my_2d = 131
his_hr = 135
his_2d = 118
rap_junk1 = 1
rap_junk2 = 1
*/``````

Results as expected.

<< Strange -- I thought I'd posted this earlier. >>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.