The challenge is this :
- Generate a random floating point number using only the default C++ libraries.
— hartnell
The challenge is this :
— hartnell
This wasn't as easy as just switching to the float data type.
But here's what my results:
#include <iostream> using namespace std; int main() { srand((unsigned)time(0)); float random_float = (float)rand()/100.0; cout << random_float << endl; return 0; }
This wasn't as easy as just switching to the float data type.
That's what I tried first too. :)
float random_float = (float)rand()/100.0;
Is the (float) in front of the rand() function some type of type casting?
— hartnell
Yeh, I think that's what it's called.
It converts the value of rand() to float. I don't even think that it's needed when I think about it.
I might be wrong, so anyone feel free to correct me. :)
I couldn't get it to work. The random number generated doesn't seem to be seeded properly and therefore the number increases as the system clock counts. Arg. I don't know how to solve this.
— hartnell
float Random()
{
static bool seed = true;
if (seed)
{
srand((unsigned)time(0));
seed = false;
}
return (float)rand() / (RAND_MAX + 1);
}
Although, for speed, the user should manually seed the thing… i haven't actually tested this but i use this in my iPhone game, so i certainly hope it works ;) It should return a value in the range [0,1) (or however you write that 1 is not included in the range)… I will make a test of this some day.
(float) casts a value to a floating point number yes, otherwise you will be stuck with integers. If (float) does not have higher precedence then / then this thing will only return 0 as RAND_MAX+1 is larger then any value returned by rand(), and an integer division rounds down.
For the curious of you out there, the static bool seed = true line is only executed the first time the function is called.
Ohh, the static keyword makes the variable just being declared once.
That might be useful if in Game Programming if you execute the function in the game loop.
But it didn't work when I tested it. It returned 0.
#include <iostream> #define RAND_MAX 100 using namespace std; float Random() { static bool seed = true; if (seed) { srand((unsigned)time(0)); seed = false; } return (float)rand() / (RAND_MAX + 1); } int main() { float random_number; cout << random_number << endl; return 0; }
RAND_MAX is defined in the C++ libraries. You should not redefine it.
Lol, I couldn't know. :p
Anyways, it still returns 0 in my program when I removed the define.
I am not sure what you are doing wrong, so here is an example that gives me random numbers between 0 and 1
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
float Random()
{
static bool seed = true;
if ( seed )
{
srand(time(NULL));
seed = false;
}
return (float)rand() / RAND_MAX;
}
int main( int numArgs, char** args )
{
for ( int i = 0; i < 5; ++i )
{
cout << Random() << endl;
}
};
Probably forgot ctime. Works now. ^^
How could you generate a higher number then? For example between 0 and 10?