ID:277928
 
Whats the best way to do what DM does, in C/C++ for probability. ie
if(prob(number))
I cant find any existing function, so what would be the best way to program something similar?
I don't remember what the name of the randomization function is in C/C++, so I'll just use rand(). This should work:
//I *think* it should be bool. I'm not sure.
bool prob(int n)
{
//The addition of one is to make sure that prob(0) is
//always going to return false.
return n >= ((rand() + 1) % 100);
}
In response to Popisfizzy
Then i guess you don't know what i need to #include to use rand()(meaning the actual randomizer)?
In response to Chibi-Gohan0
#include <cstdlib>

That has the rand function.

George Gough
In response to KodeNerd
KodeNerd wrote:
#include <cstdlib>

That has the rand function.

George Gough

Thanks, i dont seem to have it though, know where i can get it?
In response to Chibi-Gohan0
In response to A.T.H.K
To clarify, cstdlib is suposed to be a standard lib in visual C++, which i have. But i dont have the file.
In response to Chibi-Gohan0
In response to A.T.H.K
I saw most of those, and if you take note to the first one is what i was talking about where the guy redirected him to stdlib.h. Anyway, Thanks.
In response to A.T.H.K
Thanks for you help, i found the problem: cstdlib is for .cpp files only. i was using a .c file >.<
In response to Chibi-Gohan0
Hehe nice i didn't really help lol, have fun.
In response to Popisfizzy
Now that i have everything i need for rand(). Your function isnt working. i tested with if(prob(0)) and the following line was still executed(and actually, its executed every time no matter what the input).
In response to Chibi-Gohan0
Do not forget to seed the random function generator.

George Gough
In response to KodeNerd
Oh?
In response to Chibi-Gohan0
The function is set to the default seed and will always give the same list of results unless you set the seed. I think the function is srand(unsigned int) or something along that line.

Setting the seed once is best IIRC.

George Gough
In response to KodeNerd
So, is the seed inside the paramaters of srand() or is srand(paramater) the seed itself?
In response to Chibi-Gohan0
The function is called as so:

const unsigned int some_number = 10;
srand(some_number);


George Gough
In response to KodeNerd
Alright, one last thing. I get the following error after that is compiled:
error: void value not ignored as it ought to be
In response to Chibi-Gohan0
Can you post the line that has that warning on it?

George Gough
In response to KodeNerd
Its the one Pop made:
bool prob(int n) {
//The addition of one is to make sure that prob(0) is
//always going to return false.
const unsigned int num = 10;
return n >= ((srand(num) + 1) % 100); //<-------
}
Page: 1 2 3