Random()

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Random()

Post by Debeli »

Hey,i want to make every time camera hits a node to set a node at a random poition,i tried like this but it doesnt work

Code: Select all

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

Code: Select all

int count =0;
if(collision2(node,camera,20))
{
++count
};
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I think you should do some researches on rand()... :roll:

1st why do you cast to float when x is an integer ??? :shock:
2nd why do you divide by RAND_MAX (and then multiply by 1200) ??? :shock:

well, try this:

Code: Select all

// seed
srand(time(NULL));

// get random from 0 to 1199
f32 x = rand() % 1200;

// set x-position
node->setPosition(vector3df(x,300,200));
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

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:

Code: Select all

srand(time(NULL));
int x = ((float)rand()/(float)RAND_MAX)*1200.0f;
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

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:

Code: Select all

srand(time(NULL));
int x = ((float)rand()/(float)RAND_MAX)*1200.0f;
I know what you mean, but I see no need for this... ;)
and AFAIK if you realy want to use it this way, then it should be:

Code: Select all

int x = (float)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min;
but I realy see no advantage against:

Code: Select all

int x = rand() % (range_max - range_min) + range_min;
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

I don't see an advantage in this either, maybe he didn't mean to cast it to an int? If he meant to keep it as a float, it would make a lot more sense.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

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... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Debeli
Posts: 31
Joined: Thu Jan 07, 2010 2:32 pm

Post by Debeli »

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 :?:
Post Reply