User forums > General (but related to Code::Blocks)
random function question
Deamon:
--- Quote from: TDragon on April 04, 2006, 02:55:37 am ---srand(time(0)); is a function call. Function calls must be inside other functions or used as expressions -- they don't qualify as statements in the global scope.
--- End quote ---
Oh yeah forgot that we are talking about a function call here LOL
I guess it's late here again :)
--- Quote ---That's correct, there is no function in ANSI C that returns a random number within a custom range.
--- End quote ---
Bump! That sucks. Man i love the borland guys. They make such usefull stuff. The ansi guys feel the same:
http://tigcc.ticalc.org/doc/stdlib.html#random
And thanks for the help guys. Searched the net and found everything just not what i needed. What is not a miracle cose as you told me there is no specialized function for it. LOL
regards,
Deamon
Ceniza:
I still wonder why you posted that link to the gcc port for Texas Instruments calcs...
What's so true is how ppl get used to those extra functions provided by Borland :)
thomas:
--- Quote from: Deamon on April 04, 2006, 05:54:15 am ---
--- Quote ---That's correct, there is no function in ANSI C that returns a random number within a custom range.
--- End quote ---
Bump! That sucks. Man i love the borland guys. They make such usefull stuff.
--- End quote ---
Actually that doesn't suck at all. You can write your own trivial wrapper function which fits your needs exactly. It may be arbitrarily precise (like the above one) or abitrarily efficient (like for example using & if you need a less-than-some-power-of-two random).
For example, the above function is guaranteed to return a good random distribution. However, other solutions are possible which are on the order of 2-10 times faster but which might not have the same statistical properties (rand % a + b will for example be about twice as fast, and a construct using & instead of % might be 10-20 times as fast).
For applications which require a good random distribution, using % and & was an extremely bad idea back in the old days (1970-2000), as in the good old standard C generator the lower bits were not random at all (and % isn't either, if you are pedantic)!
Practically every rand() implementation uses MT these days, and most applications don't need perfect distributions, so that is fine either way... :)
If you use whatever the Borland guys made up, then you are bound to whatever they decided was right. This may not be what you want.
me22:
--- Quote from: Takeshi Miya on April 04, 2006, 01:28:46 am ---stdlib++'s <iostream> is doing nasty things here. :shock:
--- End quote ---
Actually, C++ headers are allowed to include as many or as few other C++ headers as they like, so it's perfectly acceptable for it to do that. This is the reason why using namespace std; is dangerous (since you have no idea which headers are being brought into scope) and why you are supposed to include the headers declaring anything you use, since you can't even ( technically ) rely on <iostream> including <istream> and <ostream>.
As for the original point of this thread:
--- Code: ---#include <ctime>
#include <cstdlib>
void randomize() { std::srand( std::time(0) ); }
int random(int upper) { return std::rand()%(upper+1); }
--- End code ---
( if you're using C, s/std:://g and s/<c(.+?)>/<$1.h>/g )
Note, however, that the % method of getting random numbers is actually a fairly bad pseudo-random with most crts.
The better way:
--- Code: ---double random() { std::rand()/(RAND_MAX+1.); }
--- End code ---
then n*random() will give you a decently pseudo-random number in [0,n)
If you want really good (pseudo-)random numbers, then use http://www.boost.org/libs/random
takeshimiya:
--- Quote from: me22 on April 05, 2006, 05:43:52 am ---
--- Quote from: Takeshi Miya on April 04, 2006, 01:28:46 am ---stdlib++'s <iostream> is doing nasty things here. :shock:
--- End quote ---
Actually, C++ headers are allowed to include as many or as few other C++ headers as they like, so it's perfectly acceptable for it to do that.
--- End quote ---
It's probable that you didn't read the entire post.
The point is not that <iostream> is bringing to scope (to std::) some C++ headers, but the point is that it is bringing to scope C headers, therefore making really useless the whole C++ headers for std qualifiers (cstdlib, ctime, c*...), and also helping to write non-portable programs.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version