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?
ID:277928
Aug 7 2008, 11:44 am
|
|
Aug 7 2008, 11:52 am
|
|
I don't remember what the name of the randomization function is in C/C++, so I'll just use rand(). This should work:
|
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> Thanks, i dont seem to have it though, know where i can get it? |
In response to Chibi-Gohan0
|
|
http://google.com - here
|
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
|
|
http://compsci.ca/v3/viewtopic.php?t=15274
http://www.cplusplus.com/reference/clibrary/cstdlib/ http://answers.yahoo.com/question/ index?qid=20071008061305AAmOkQw http://lxr.go-oo.org/source/util/std2/stl/cstdlib http://ib.cnea.gov.ar/~oop/biblio/libstdc++/ cstdlib-source.html Hope they help, mayeb you should start from the bottom link :P |
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; 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); //<------- } |