int x;
srand(time(NULL));
rand();
x=(static_cast<float>(rand())/ RAND_MAX)*1200;
node->setPosition(vector3df(x,300,200));
And another thing i use get distance to see if collison happens between the camera and when i tried to count how many times collison happens but when it hits it once my counter show like 5-6 times how sould i fix this problem
Acki, say a random number generator goes from 0 to 5000. If you define RAND_MAX as 5000 and divide a given random number by it, it will give you a decimal number between 0-1. Multiplying by 1200 and then casting back to an int will round it to a number between 0-1200. Anyway to the original poster, have you tried casting RAND_MAX to a float also? It may be that it's rounding the float to 0 or 1. Here's some code that worked for me:
Lonesome Ducky wrote:Acki, say a random number generator goes from 0 to 5000. If you define RAND_MAX as 5000 and divide a given random number by it, it will give you a decimal number between 0-1. Multiplying by 1200 and then casting back to an int will round it to a number between 0-1200. Anyway to the original poster, have you tried casting RAND_MAX to a float also? It may be that it's rounding the float to 0 or 1. Here's some code that worked for me:
hmm, maybe modulo is a little bit more expensive (didn't test it), but as long as you don't calculate billions of random numbers at a time I doubt you will ever recognise this, just in case it is more expensive...
Thanks for replies but i was thinking and i very likely tried to make it work in the wrong way...because im actually trying to make my object fall out of the sky,and the goal is to collect as many objects as you can at a limited time,soo all i need is that when you collect the object another one appers but now in a random position,which is kind of stupid because it would first time you collected set it at random position and then it will always go in thet random position.How should i deal with smoething like this